Skip to content

Instantly share code, notes, and snippets.

@sishen
Created June 4, 2012 05:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sishen/2866558 to your computer and use it in GitHub Desktop.
Save sishen/2866558 to your computer and use it in GitHub Desktop.
Dirty plugin for SpineJS.
Spine ?= require('spine')
Include =
savePrevious: ->
@constructor.records[@id].previousAttributes = @attributes()
Spine.Model.Dirty =
extended: ->
@bind 'refresh', ->
@each (record) -> record.savePrevious()
@bind 'save', (record) ->
if record.previousAttributes?
for key in record.constructor.attributes when key of record
if record[key] isnt record.previousAttributes[key]
record.trigger('change:'+key, record[key])
record.savePrevious()
@include Include
So the model object can bind the event "change:#{field} to trigger event when the field value is changed.
By default it's off and if need this feature, the model should extend Spine.Model.Dirty.
A sample case.
class User extends Spine.Model
@extend Spine.Model.Dirty
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment