When updating an object, I typically create a new object with only the changed properties and send that object. Before making any changes to the original object, I make a copy of it for comparison purposes. This can easily be adapted to use $dirty in the following way:
function generateUpdateObject(original, current) {
var modifications = {};
for (var property in original) {
if (property.indexOf("$") != 0 && original[property] !== current[property]) {
modifications[property] = current[property];
}
}
return modifications;
};
My update function then utilizes this method to obtain a fresh object containing only the modifications, assigns the primary key from the current object being manipulated, and sends it to the server. Depending on your backend setup, you may need to perform an http patch request for this process to succeed. Here is a potential implementation:
function saveChanges() {
var modifications = generateUpdateObject(vm.original, vm.current)
modifications.id = vm.original.id
$http.patch("http:/serviceURI.com (" + modifications.id + ")", modifications).then(...)
}
This code snippet was extracted from an application that leverages oData and has been slightly adjusted for this explanation. It forms part of a service that I utilize for all my oData interactions.