Skip to content

Instantly share code, notes, and snippets.

@pixelhandler
Created May 23, 2014 00:11
Show Gist options
  • Save pixelhandler/d90cd50ed3a63fd4b919 to your computer and use it in GitHub Desktop.
Save pixelhandler/d90cd50ed3a63fd4b919 to your computer and use it in GitHub Desktop.
Auto save an Ember.js model (DS.Model)
get = Ember.get
App.BaseModel = DS.Model.extend
autoSave: (->
if get(@, 'isDraft') and !(get @, 'isNew') and isReallyDirty @
Ember.run.debounce @, '_doAutoSave', 12000
).observes 'isDirty'
_doAutoSave: ->
@save() if get @, 'isDirty'
isDraft: (->
get(@, 'status') is 'draft'
).property 'status'
# Strange that model can be dirty with no changed attrs
isReallyDirty = (model) ->
hasChangingAttr = false
attrs = []
model.eachAttribute (name)-> attrs.push name
for own key, value of model.changedAttributes()
if attrs.contains key
hasChangingAttr = true
break
hasChangingAttr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment