Skip to content

Instantly share code, notes, and snippets.

@melvynhills
Created June 22, 2012 11:08
Show Gist options
  • Save melvynhills/2972123 to your computer and use it in GitHub Desktop.
Save melvynhills/2972123 to your computer and use it in GitHub Desktop.
Spine plugin for dirty model attributes
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
###
Usage example:
class Model extends Spine.Model
@configure "Model", "firstname", "lastname"
@extend Spine.Model.Dirty
class Controller extends Spine.Controller
constructor: ->
Model.bind "change:lastname", (model, lastname) =>
@log model, lastname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment