Skip to content

Instantly share code, notes, and snippets.

@Samsinite
Created October 28, 2015 17:40
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 Samsinite/369c7a4a85ff9686d13a to your computer and use it in GitHub Desktop.
Save Samsinite/369c7a4a85ff9686d13a to your computer and use it in GitHub Desktop.
Ideal ember model save function
import Ember from 'ember';
export function modelSave(subject, { successMsg, errorMsg, transitionPath, notifyLookupPath, confirmLookupPath, saveFunc }) {
if (!saveFunc) {
saveFunc = subject.save.bind(subject);
}
if (!notifyLookupPath) {
notifyLookupPath = 'service:notify'
}
return Ember.Object.create({
notify: Ember.computed(function() {
return this.container.lookup(notifyLookupPath);
}),
save() {
saveFunc.then(() => {
let notify = this.get('notify');
if (successMsg) {
this.notify.success(successMsg);
}
if (transitionPath) {
... // Needs written, would be much easier to write in ember 2.2 with the more public router API
}
}).catch((e) => {
if (errorMsg) {
this.notify.error(errorMsg);
}
throw e;
})
}
}).save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment