6
I’m working with an application where in a view
I own the product register of a company, or its customers, suppliers, etc. Each view
meets a specific area, but each view
works with diverse data and data flow, like adding new items, deleting and updating.
The problem I’ve noticed is that when the view
has many items, when executing an addition, where it is necessary to make a reload
from the list, in order to enter the new data with its respective bank id, there is a "duplicate" of the entire list for a brief moment. Roughly speaking, I insert a new client, he does the reload
from the list, apply the whole list to the end of the list that is already on view
only to remove the old list.
The code I’m using has no secret, it has $http
simple of POST
and GET
(my backend is controlled by PHP
) as in the example:
Ctrl.js
//Chamada automática
function getProduto() {
factProdutos.getProdutos().then(function (res) {
vm.produtos = res;
});
};
//Chamada da view
vm.addProduto = addProduto;
//Function - Produtos
function addProduto(id) {
var data = {id_empresa:id};
$http.post('php/mainFile.php?action=addProduto', data).then(
function(res) { getProduto(); },
function(err) { alert(feedbackError); }
);
};
Factory.js
function _getProdutos() {
return $http.get("php/getFile.php?action=getProduto").then(
function(res) { return res.data;},
function(err) {alert(feedbackError);}
);
};
To make the deletion or update no problem, because I do the process in the Database without having to reload the information, Angularjs takes care of doing this in the view. The same occurs to remove a product from the list, just using the $filter
and delete the array element.
The problem even occurs when making a new insertion, because I need id to perform future processes. I’ve read about using the same $filter logic for deletion, but instead of removing, adding the new data.
But how to identify the new data? Or compare the new list loaded with the list currently in my view
? Or, this is the best way to do this optimization, or is there a better method?
This is not a mistake, but an optimization of the data flow.
Your action
addProduto
(server) cannot return the ID?– bfavaretto
Yes, but in some cases it returns a few more fields. For example, the product has reference with a company and a section, so it will come with a few more arguments.
– celsomtrindade
In similar situations I maintain the collection of items in a specialized service (empresasService, Clientesservice) and coordinate merge of the object returned with the collection already present in memory. My views receive only references to the collection.
– OnoSendai
Any suggestions on how to merge this into my data stream @Onosendai? Suggestions are welcome. I value the convenience/agility of the code to maintain a pre-set standard by me.
– celsomtrindade
It depends a lot on your model, @Celsomtrindade - I, for example, keep the reference of the object being manipulated at the moment, and in the return of the appropriate method (PUT, DELETE, etc.) I decide the action to be taken. I expose the collection from the service, as a property, and map it directly in the Controller.
– OnoSendai
@Onosendai Any tips/models on how to do this? Because never a
service
(except Factory) especially for this type of treatment. Which may actually be more useful than I think, because if there is a possibility to keep a 'cache' of that list, I don’t need to reload it every time I access a particular view. That would be possible?– celsomtrindade
Sure - give me a few minutes to write an example.
– OnoSendai
No problem, in your time, it’s not so urgent. As I said, it’s just optimization, learning. = D
– celsomtrindade