Created
May 23, 2014 00:11
-
-
Save pixelhandler/d90cd50ed3a63fd4b919 to your computer and use it in GitHub Desktop.
Auto save an Ember.js model (DS.Model)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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