Last active
August 29, 2015 14:08
-
-
Save poteto/94498cc91e804c9883fe to your computer and use it in GitHub Desktop.
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
// Ideally you would set up a flash message service as a dependency injection | |
// (which can be a little involved), but here's a quick hack | |
// P.S. this was transpiled down from CoffeeScript, so it might look a bit funky | |
App.Post = DS.Model.extend({ | |
errorMessages: Em.A([]), | |
didBecomeDirty: Em.observer('isDirty', function() { | |
return Em.run.debounce(this, '_handleSave', 500, true); | |
}), | |
_handleSave: function() { | |
var flashes; | |
flashes = this.get('errorMessages'); | |
return this.save().then(function() { | |
return flashes.pushObject({ | |
message: 'Your post was auto saved' | |
}); | |
}).catch(function(error) { | |
if (error.status === 500) { | |
return flashes.pushObject({ | |
message: "Server error. Couldn't auto save." | |
}); | |
} | |
}).finally((function(_this) { | |
return function() { | |
return _this._clearErrorMessages(6000); | |
}; | |
})(this)); | |
}, | |
_clearErrorMessages: function(timeout) { | |
return Em.run.later(this, function() { | |
return this.get('errorMessages').clear(); | |
}, timeout); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment