diff --git a/app/controllers/collections/create.js b/app/controllers/collections/create.js index 37192de..8b27025 100644 --- a/app/controllers/collections/create.js +++ b/app/controllers/collections/create.js @@ -11,6 +11,8 @@ export default Controller.extend(ValidationMixin, { CollectionSpeciesValidations, DatasheetValidations, + hasMany: ['collectionSpecies', 'datasheets'], + options: computed('projectOptions', 'studyLocationOptions', 'collectionTypeOptions', 'collectionMethodOptions', 'speciesOptions', 'adfgPermitOptions', function() { diff --git a/app/controllers/collections/detail/edit.js b/app/controllers/collections/detail/edit.js index 72c586c..df9f0ee 100644 --- a/app/controllers/collections/detail/edit.js +++ b/app/controllers/collections/detail/edit.js @@ -11,6 +11,9 @@ export default Controller.extend(ValidationMixin, { CollectionSpeciesValidations, DatasheetValidations, + hasMany: ['collectionSpecies', 'datasheets'], + safeNavigate: false, + options: computed('projectOptions', 'studyLocationOptions', 'collectionTypeOptions', 'collectionMethodOptions', 'speciesOptions', 'adfgPermitOptions', function() { @@ -27,15 +30,21 @@ export default Controller.extend(ValidationMixin, { actions: { onSave(changesets) { const postSave = () => { + this.set('safeNavigate', true); // Use the model's ID here because of the ArrayProxy in the route this.transitionToRoute('collections.detail', this.get('model.id')); + this.set('safeNavigate', false); + console.log('save'); }; return this.validationSave(changesets, postSave); }, onCancel(changesets) { const postCancel = () => { + this.set('safeNavigate', true); // Use the model's ID here because of the ArrayProxy in the route - return this.transitionToRoute('collections.detail', this.get('model.id')); + this.transitionToRoute('collections.detail', this.get('model.id')); + this.set('safeNavigate', false); + console.log('cancel'); }; return this.validationCancel(changesets, postCancel); }, diff --git a/app/mixins/cleanup-form.js b/app/mixins/cleanup-form.js new file mode 100644 index 0000000..fdf1032 --- /dev/null +++ b/app/mixins/cleanup-form.js @@ -0,0 +1,36 @@ +import Ember from 'ember'; + +const { Mixin, run: { once } } = Ember; + +export default Mixin.create({ + actions: { + willTransition(transition) { + if (!this.get('controller.safeNavigate')) { + if (confirm('Any unsaved changes will be discarded.')) { + let model = this.get('controller.model'); + let hasMany = this.get('controller.hasMany'); + + hasMany.forEach((relationship) => { + model.get(relationship).forEach((r) => { + once(this, () => { + if (r.get('isNew')) { + r.deleteRecord(); + } else { + r.rollbackAttributes(); + } + }, this); + }); + }); + + if (model.get('isNew')) { + model.deleteRecord(); + } + } else { + transition.abort(); + return false + } + } + return true; + }, + }, +}); diff --git a/app/routes/collections/create.js b/app/routes/collections/create.js index 2ecd267..cd56f73 100644 --- a/app/routes/collections/create.js +++ b/app/routes/collections/create.js @@ -1,8 +1,9 @@ import Ember from 'ember'; +import CleanupFormMixin from 'ccdb-web/mixins/cleanup-form'; const { Route, RSVP } = Ember; -export default Route.extend({ +export default Route.extend(CleanupFormMixin, { model() { const store = this.get('store'); return RSVP.hash({ diff --git a/app/routes/collections/detail/edit.js b/app/routes/collections/detail/edit.js index fc99b16..6237884 100644 --- a/app/routes/collections/detail/edit.js +++ b/app/routes/collections/detail/edit.js @@ -1,8 +1,9 @@ import Ember from 'ember'; +import CleanupFormMixin from 'ccdb-web/mixins/cleanup-form'; const { Route, RSVP } = Ember; -export default Route.extend({ +export default Route.extend(CleanupFormMixin, { model() { const store = this.get('store'); const model = this.modelFor('collections.detail'); diff --git a/config/environment.js b/config/environment.js index 6132d43..abd9a84 100644 --- a/config/environment.js +++ b/config/environment.js @@ -26,6 +26,7 @@ module.exports = function(environment) { if (environment === 'development') { ENV.APP.API_HOST = 'http://localhost:8000'; ENV.APP.API_NAMESPACE = 'api/v1'; + ENV.APP.LOG_TRANSITIONS = true; } if (environment === 'test') {