-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send changedAttributes to Backbone.sync #391
Conversation
* When changedAttributes is called from within Backbone.sync it returns false as the change method overwrites _previousAttributes. This makes it hard to send only the updated attributes to the server when a model is updated.
I'd love to see this merged. |
Sending only updated attributes? Sounds like PATCH method to me. :) |
This has been discussed several times before -- check the issues. Backbone by default PUTs the full representation of the model -- if you'd like to only send the change set, feel free to override |
Understood, however overriding sync to only send changed attributes is currently quite tricky. This change would make it much more straightforward. |
This is indeed issue. Calling changedAttributes() inside CUSTOM sync ALWAYS returns false. |
At least an example how to go about doing this in a custom |
This could work as a makeshift solution:
|
Another way of kinda doing the same thing: /**
* This assumes we're just overriding the sync method of a single model
* and that we're only concerned with poking at changed data for update
* events sent to the server
*/
sync: function (method model options){
if(method === "update"){
var changedData = {};
_.each(options.changes, function(changed, key){
if(changed){ changedData[key] = model.get(key); }
});
// If you set data, you need to declare your content type
// Note I use PATCH because my backend uses tastypie
// it's not strictly required though
options = _.extend(options, {type: "PATCH",
contentType: 'application/json',
data: JSON.stringify(changedData)});
}
Backbone.sync(method, model, options);
} The "sneaky" bit here is putting data in the options. Keep in mind that if you have to do emulateJSON that changes what you'll have to set your options.data to. |
it returns false as the change method overwrites
_previousAttributes. This makes it hard to send only
the updated attributes to the server when a model is
updated.