Skip to content

Instantly share code, notes, and snippets.

@felipap
Last active December 21, 2015 20:09
Show Gist options
  • Save felipap/6359302 to your computer and use it in GitHub Desktop.
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.
// 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