Last active
December 21, 2015 20:09
-
-
Save felipap/6359302 to your computer and use it in GitHub Desktop.
Extending patch:true behaviour in a Backbone application, to update only the given attributes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extend PATCH:true option of Backbone. | |
// When model.save([attrs], {patch:true}) is called: | |
// - the method is changed to PUT; | |
// - the data sent is a hash with the passed attributes and their values; | |
var originalSync = Backbone.sync; | |
Backbone.sync = function(method, model, options) { | |
if (method === 'patch' && options.attrs instanceof Array) { | |
// pop attributes and add their values | |
while (e = options.attrs.pop()) | |
options.attrs[e] = model.get(e); | |
options.type = 'PUT'; | |
// turn options.attrs into an Object | |
options.attrs = _.extend({}, options.attrs); | |
} | |
return originalSync(method, model, options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment