0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much Mike.. It have changed my code according to your explanation. it worked.. :).. thank You!!
Great! Feel free to upvote and accept the answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.