Created
October 28, 2015 17:40
-
-
Save Samsinite/369c7a4a85ff9686d13a to your computer and use it in GitHub Desktop.
Ideal ember model save function
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
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