open-vault/ui/app/components/edit-form.js
Matthew Irish b32da5daad
use ember-concurrency test waiter (#6555)
* add ember-concurrency-test-waiter

* use withTestWaiter modifier in various parts of the application

* more withTestWaiter

* await instead of return for a few helpers

* update ember-cli-flash

* run ember generate ember-cli-flash

* remove withFlash helper

* fix typo and replication tests
2019-04-10 09:36:32 -05:00

51 lines
1.3 KiB
JavaScript

import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { task } from 'ember-concurrency';
import DS from 'ember-data';
export default Component.extend({
flashMessages: service(),
// public API
model: null,
successMessage: 'Saved!',
deleteSuccessMessage: 'Deleted!',
deleteButtonText: 'Delete',
saveButtonText: 'Save',
cancelLink: null,
/*
* @param Function
* @public
*
* Optional param to call a function upon successfully saving a model
*/
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()
.withTestWaiter(),
willDestroy() {
let model = this.get('model');
if ((model.get('isDirty') && !model.isDestroyed) || !model.isDestroying) {
model.rollbackAttributes();
}
},
});