Reputation: 219
I have a save method. On click of a SAVE I only want to send the changed fields instead of everything Since i have more then 50 fields on my page. So its very heavy to send all the fields everytime.
Api.Admin.update({
obsoleteDate : AdminEdit.ObsoleteDate.$dirty== true?$scope.editObsoleteDate:""
});
above I am checking with field dirty value true or false. but i don't want to send value as empty if value is not changed. What other way to that that?
Upvotes: 0
Views: 2819
Reputation: 5176
I usually build an object with just the changed properties and send that object. I make a copy of the original object before editing it and then use that to compare, but you could easily adapt this to us $dirty. Something along these lines:
function getUpdateObject(orig, current) {
var changes = {};
for (var prop in orig) {
if (prop.indexOf("$") != 0 && orig[prop] !== current[prop]) {
changes[prop] = current[prop];
}
}
return changes ;
};
Then my update code calls this function to get a new object with just my changes, assigns whatever the primary key is from what I'm working with to that object and sends that to the server. I have no idea what your back end is, but you'll most likely need to do an http patch in order for this to work. So something like this:
function save() {
var changes = getUpdateObject(vm.orig, vm.current)
changes.id = vm.orig.id
$http.patch("http:/serviceURI.com (" + changes.id + ")", changes).then(...)
}
I pulled this code out of an app that uses oData and modified it a bit for this answer, but all of this code exists in a service that I use for all of my oData interactions.
Upvotes: 2