2018-09-25 16:28:26 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import Component from '@ember/component';
|
2018-07-19 01:59:04 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import DS from 'ember-data';
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
export default Component.extend({
|
|
|
|
flashMessages: service(),
|
2018-07-19 01:59:04 +00:00
|
|
|
|
|
|
|
// public API
|
|
|
|
model: null,
|
|
|
|
successMessage: 'Saved!',
|
|
|
|
deleteSuccessMessage: 'Deleted!',
|
|
|
|
deleteButtonText: 'Delete',
|
2018-08-16 17:48:24 +00:00
|
|
|
saveButtonText: 'Save',
|
|
|
|
cancelLink: null,
|
2018-07-19 01:59:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @param Function
|
|
|
|
* @public
|
|
|
|
*
|
2018-08-16 17:48:24 +00:00
|
|
|
* Optional param to call a function upon successfully saving a model
|
2018-07-19 01:59:04 +00:00
|
|
|
*/
|
|
|
|
onSave: () => {},
|
|
|
|
|
|
|
|
save: task(function*(model, options = { method: 'save' }) {
|
|
|
|
let { method } = options;
|
|
|
|
let messageKey = method === 'save' ? 'successMessage' : 'deleteSuccessMessage';
|
|
|
|
try {
|
|
|
|
yield model[method]();
|
|
|
|
} catch (err) {
|
|
|
|
// err will display via model state
|
|
|
|
// AdapterErrors are handled by the error-message component
|
|
|
|
if (err instanceof DS.AdapterError === false) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.get('flashMessages').success(this.get(messageKey));
|
|
|
|
yield this.get('onSave')({ saveType: method, model });
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
willDestroy() {
|
|
|
|
let model = this.get('model');
|
|
|
|
if ((model.get('isDirty') && !model.isDestroyed) || !model.isDestroying) {
|
|
|
|
model.rollbackAttributes();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|