Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poteto/94498cc91e804c9883fe to your computer and use it in GitHub Desktop.
Save poteto/94498cc91e804c9883fe to your computer and use it in GitHub Desktop.
// 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