Refactor species edit

This commit is contained in:
Matthew Ryan Dillon 2015-11-03 11:27:22 -07:00
parent d1e3d05db2
commit 4dbfb73b3c
8 changed files with 60 additions and 41 deletions

View file

@ -1,29 +1,31 @@
import Ember from 'ember';
import ajaxError from '../../../../utils/ajax-error';
export default Ember.Controller.extend({
const { Controller } = Ember;
export default Controller.extend({
actions: {
save: function() {
let species = this.get('model');
const model = this.get('model');
if (species.get('hasDirtyAttributes')) {
species.save().then((species) => {
this.transitionToRoute('protected.species.show', species);
if (model.get('hasDirtyAttributes')) {
model.save().then((model) => {
this.transitionToRoute('protected.species.show', model);
}, () => {
ajaxError(species.get('errors'), this.get('flashMessages'));
ajaxError(model.get('errors'), this.get('flashMessages'));
});
} else {
this.transitionToRoute('protected.species.show', species);
this.transitionToRoute('protected.species.show', model);
}
},
cancel: function() {
let species = this.get('model');
const model = this.get('model');
species.get('errors').clear();
species.rollbackAttributes();
model.get('errors').clear();
model.rollbackAttributes();
this.transitionToRoute('protected.species.show', species);
this.transitionToRoute('protected.species.show', model);
},
},

View file

@ -1,7 +1,9 @@
import Ember from 'ember';
export default Ember.Route.extend({
currentUser: Ember.inject.service('session-account'),
const { Route, inject: { service } } = Ember;
export default Route.extend({
currentUser: service('session-account'),
beforeModel: function(transition) {
this._super(transition);
@ -12,17 +14,10 @@ export default Ember.Route.extend({
});
},
afterModel: function(species) {
if (!species.get('canEdit')) {
this.transitionTo('species.show', species.get('id'));
afterModel: function(model) {
if (!model.get('canEdit')) {
this.transitionTo('species.show', model.get('id'));
}
},
setupController: function(controller, model) {
controller.set('model', model);
this.get('currentUser.account').then((user) => {
controller.set('metaData', user.get('metaData'));
});
},
});

View file

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