From 9cb030ab0237623ab9cf264b85192ab2d72c0d34 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Thu, 5 Nov 2015 14:18:59 -0700 Subject: [PATCH] Refactor characteristics/new --- app/mixins/save-model.js | 7 +++-- .../characteristics/edit/controller.js | 3 +- .../characteristics/new/controller.js | 31 ++++--------------- .../protected/characteristics/new/route.js | 28 ++++------------- .../characteristics/new/template.hbs | 4 +-- app/pods/protected/species/edit/controller.js | 3 +- app/pods/protected/species/new/controller.js | 3 +- 7 files changed, 24 insertions(+), 55 deletions(-) diff --git a/app/mixins/save-model.js b/app/mixins/save-model.js index 97cc97b..53efda9 100644 --- a/app/mixins/save-model.js +++ b/app/mixins/save-model.js @@ -4,12 +4,13 @@ import ajaxError from '../utils/ajax-error'; const { Mixin } = Ember; export default Mixin.create({ - fallbackRoute: null, + fallbackRouteSave: null, + fallbackRouteCancel: null, actions: { save: function(properties) { const model = this.get('model'); - const fallbackRoute = this.get('fallbackRoute'); + const fallbackRoute = this.get('fallbackRouteSave'); model.setProperties(properties); @@ -30,7 +31,7 @@ export default Mixin.create({ model.get('errors').clear(); model.rollbackAttributes(); - this.transitionToRoute(this.get('fallbackRoute'), model); + this.transitionToRoute(this.get('fallbackRouteCancel'), model); }, }, }); diff --git a/app/pods/protected/characteristics/edit/controller.js b/app/pods/protected/characteristics/edit/controller.js index 326bca4..0b83924 100644 --- a/app/pods/protected/characteristics/edit/controller.js +++ b/app/pods/protected/characteristics/edit/controller.js @@ -5,5 +5,6 @@ const { Controller } = Ember; export default Controller.extend(SaveModel, { // Required for SaveModel mixin - fallbackRoute: 'protected.characteristics.show', + fallbackRouteSave: 'protected.characteristics.show', + fallbackRouteCancel: 'protected.characteristics.index', }); diff --git a/app/pods/protected/characteristics/new/controller.js b/app/pods/protected/characteristics/new/controller.js index 1d9eef6..0b83924 100644 --- a/app/pods/protected/characteristics/new/controller.js +++ b/app/pods/protected/characteristics/new/controller.js @@ -1,29 +1,10 @@ import Ember from 'ember'; -import ajaxError from '../../../../utils/ajax-error'; +import SaveModel from '../../../../mixins/save-model'; -export default Ember.Controller.extend({ - actions: { - save: function() { - let characteristic = this.get('model'); +const { Controller } = Ember; - if (characteristic.get('hasDirtyAttributes')) { - characteristic.save().then((characteristic) => { - this.transitionToRoute('protected.characteristics.show', characteristic); - }, () => { - ajaxError(characteristic.get('errors'), this.get('flashMessages')); - }); - } else { - characteristic.destroyRecord().then(() => { - this.transitionToRoute('protected.characteristics.index'); - }); - } - }, - - cancel: function() { - this.get('model').destroyRecord().then(() => { - this.transitionToRoute('protected.characteristics.index'); - }); - }, - - }, +export default Controller.extend(SaveModel, { + // Required for SaveModel mixin + fallbackRouteSave: 'protected.characteristics.show', + fallbackRouteCancel: 'protected.characteristics.index', }); diff --git a/app/pods/protected/characteristics/new/route.js b/app/pods/protected/characteristics/new/route.js index 34165dd..c287cf9 100644 --- a/app/pods/protected/characteristics/new/route.js +++ b/app/pods/protected/characteristics/new/route.js @@ -1,30 +1,14 @@ import Ember from 'ember'; +import ElevatedAccess from '../../../../mixins/elevated-access'; -export default Ember.Route.extend({ - currentUser: Ember.inject.service('session-account'), +const { Route } = Ember; - beforeModel: function(transition) { - this._super(transition); - this.get('currentUser.account').then((user) => { - if (user.get('isReader')) { - this.transitionTo('protected.characteristics.index'); - } - }); - }, +export default Route.extend(ElevatedAccess, { + // Required for ElevatedAccess mixin + fallbackRouteBefore: 'protected.characteristics.index', + fallbackRouteAfter: 'protected.characteristics.show', model: function() { return this.store.createRecord('characteristic'); }, - - actions: { - willTransition: function(/*transition*/) { - const controller = this.get('controller'); - const characteristic = controller.get('model'); - - if (characteristic.get('isNew')) { - characteristic.destroyRecord(); - } - }, - }, - }); diff --git a/app/pods/protected/characteristics/new/template.hbs b/app/pods/protected/characteristics/new/template.hbs index 3f1fe65..f91ed1c 100644 --- a/app/pods/protected/characteristics/new/template.hbs +++ b/app/pods/protected/characteristics/new/template.hbs @@ -1,6 +1,6 @@ {{ protected/characteristics/characteristic-form characteristic=model - save="save" - cancel="cancel" + on-save=(action "save") + on-cancel=(action "cancel") }} diff --git a/app/pods/protected/species/edit/controller.js b/app/pods/protected/species/edit/controller.js index 5f7591b..3b524f9 100644 --- a/app/pods/protected/species/edit/controller.js +++ b/app/pods/protected/species/edit/controller.js @@ -5,5 +5,6 @@ const { Controller } = Ember; export default Controller.extend(SaveModel, { // Required for SaveModel mixin - fallbackRoute: 'protected.species.show', + fallbackRouteSave: 'protected.species.show', + fallbackRouteCancel: 'protected.species.show', }); diff --git a/app/pods/protected/species/new/controller.js b/app/pods/protected/species/new/controller.js index 5f7591b..5addc72 100644 --- a/app/pods/protected/species/new/controller.js +++ b/app/pods/protected/species/new/controller.js @@ -5,5 +5,6 @@ const { Controller } = Ember; export default Controller.extend(SaveModel, { // Required for SaveModel mixin - fallbackRoute: 'protected.species.show', + fallbackRouteSave: 'protected.species.show', + fallbackRouteCancel: 'protected.species.index', });