2018-07-19 01:59:04 +00:00
|
|
|
import Ember from 'ember';
|
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import DS from 'ember-data';
|
|
|
|
|
|
|
|
const { inject } = Ember;
|
|
|
|
export default Ember.Component.extend({
|
|
|
|
flashMessages: inject.service(),
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|