open-vault/ui/app/mixins/unload-model-route.js
Jordan Reimer 52fe56ec87
Eslint prefer-const (#17864)
* adds prefer-const to eslint config and runs fixer

* reverts unintended change
2022-11-09 15:15:31 -08:00

29 lines
892 B
JavaScript

import Mixin from '@ember/object/mixin';
import removeRecord from 'vault/utils/remove-record';
// removes Ember Data records from the cache when the model
// changes or you move away from the current route
export default Mixin.create({
modelPath: 'model',
unloadModel() {
const { modelPath } = this;
/* eslint-disable-next-line ember/no-controller-access-in-routes */
const model = this.controller.get(modelPath);
// error is thrown when you attempt to unload a record that is inFlight (isSaving)
if (!model || !model.unloadRecord || model.isSaving) {
return;
}
removeRecord(this.store, model);
model.destroy();
// it's important to unset the model on the controller since controllers are singletons
this.controller.set(modelPath, null);
},
actions: {
willTransition() {
this.unloadModel();
return true;
},
},
});