Skip to content

Instantly share code, notes, and snippets.

@aligo
Created June 3, 2012 10:30
Show Gist options
  • Save aligo/2862960 to your computer and use it in GitHub Desktop.
Save aligo/2862960 to your computer and use it in GitHub Desktop.
save_changes_common =
sync: (method, model, options) ->
if method == 'update' && model.changedAttributes()
options.data = JSON.stringify(model.changedAttributes())
options.contentType = 'application/json';
Backbone.sync.call(this, method, model, options)
save_changes_model =
saveChanges: (key, value, options) ->
self = this
if _.isObject(key) || key == null
attrs = key
options = value
else
attrs = {}
attrs[key] = value
`options = options ? _.clone(options) : {}`
_.extend options, { wait: true }
actually_changed = _.any attrs, (value, key)->
value != self.get key
if actually_changed
@save attrs, options
@set attrs
_.extend Backbone.Model.prototype, save_changes_common, save_changes_model
_.extend Backbone.Collection.prototype, save_changes_common
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment