diff --git a/app/mixins/save-model.js b/app/mixins/save-model.js new file mode 100644 index 0000000..5db43fa --- /dev/null +++ b/app/mixins/save-model.js @@ -0,0 +1,31 @@ +import Ember from 'ember'; +import ajaxError from '../utils/ajax-error'; + +export default Ember.Mixin.create({ + actions: { + save: function() { + const model = this.get('model'); + const fallbackRoute = this.get('fallbackRoute'); + + if (model.get('hasDirtyAttributes')) { + model.save().then((model) => { + this.transitionToRoute(fallbackRoute, model); + }, () => { + ajaxError(model.get('errors'), this.get('flashMessages')); + }); + } else { + this.transitionToRoute(fallbackRoute, model); + } + }, + + cancel: function() { + const model = this.get('model'); + const fallbackRoute = this.get('fallbackRoute'); + + model.get('errors').clear(); + model.rollbackAttributes(); + + this.transitionToRoute(fallbackRoute, model); + }, + }, +}); diff --git a/app/pods/protected/species/edit/controller.js b/app/pods/protected/species/edit/controller.js index 02c6354..5f7591b 100644 --- a/app/pods/protected/species/edit/controller.js +++ b/app/pods/protected/species/edit/controller.js @@ -1,32 +1,9 @@ import Ember from 'ember'; -import ajaxError from '../../../../utils/ajax-error'; +import SaveModel from '../../../../mixins/save-model'; const { Controller } = Ember; -export default Controller.extend({ - actions: { - save: function() { - const model = this.get('model'); - - if (model.get('hasDirtyAttributes')) { - model.save().then((model) => { - this.transitionToRoute('protected.species.show', model); - }, () => { - ajaxError(model.get('errors'), this.get('flashMessages')); - }); - } else { - this.transitionToRoute('protected.species.show', model); - } - }, - - cancel: function() { - const model = this.get('model'); - - model.get('errors').clear(); - model.rollbackAttributes(); - - this.transitionToRoute('protected.species.show', model); - }, - - }, +export default Controller.extend(SaveModel, { + // Required for SaveModel mixin + fallbackRoute: 'protected.species.show', }); diff --git a/tests/unit/mixins/save-model-test.js b/tests/unit/mixins/save-model-test.js new file mode 100644 index 0000000..63ff660 --- /dev/null +++ b/tests/unit/mixins/save-model-test.js @@ -0,0 +1,12 @@ +import Ember from 'ember'; +import SaveModelMixin from '../../../mixins/save-model'; +import { module, test } from 'qunit'; + +module('Unit | Mixin | save model'); + +// Replace this with your real tests. +test('it works', function(assert) { + var SaveModelObject = Ember.Object.extend(SaveModelMixin); + var subject = SaveModelObject.create(); + assert.ok(subject); +});