Refactor characteristics/new

This commit is contained in:
Matthew Dillon 2015-11-05 14:18:59 -07:00
parent 7106696533
commit 9cb030ab02
7 changed files with 24 additions and 55 deletions

View file

@ -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);
},
},
});

View file

@ -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',
});

View file

@ -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',
});

View file

@ -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();
}
},
},
});

View file

@ -1,6 +1,6 @@
{{
protected/characteristics/characteristic-form
characteristic=model
save="save"
cancel="cancel"
on-save=(action "save")
on-cancel=(action "cancel")
}}

View file

@ -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',
});

View file

@ -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',
});