Skip to content

Instantly share code, notes, and snippets.

@juggy
Created June 13, 2011 16:25
Show Gist options
  • Save juggy/1023110 to your computer and use it in GitHub Desktop.
Save juggy/1023110 to your computer and use it in GitHub Desktop.
Revert to last saved version using Backbone.js
Skepic.EventedModel = Backbone.Model.extend
initialize: ->
Backbone.Model.prototype.initialize.apply(@, arguments)
_.bindAll(@, "mark_to_revert", "revert")
@mark_to_revert()
save : (attrs, options)->
self = this
options || (options = {})
success = options.success
options.success = (resp) ->
self.trigger("save:success", self)
success(self, resp) if (success)
self.mark_to_revert()
@trigger("save", this)
value = Backbone.Model.prototype.save.call(this, attrs, options)
return value
mark_to_revert: ->
@_revertAttributes = _.clone @attributes
revert: ->
@set(@_revertAttributes, {silent : true}) if @_revertAttributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment