Using mixins on new species
This commit is contained in:
parent
76942a10d0
commit
cc85ee6f27
5 changed files with 29 additions and 49 deletions
app
|
@ -5,6 +5,9 @@ const { Mixin , inject: { service } } = Ember;
|
|||
export default Mixin.create({
|
||||
currentUser: service('session-account'),
|
||||
|
||||
fallbackRouteBefore: null,
|
||||
fallbackRouteAfter: null,
|
||||
|
||||
beforeModel: function(transition) {
|
||||
this._super(transition);
|
||||
this.get('currentUser.account').then((user) => {
|
||||
|
@ -19,4 +22,15 @@ export default Mixin.create({
|
|||
this.transitionTo(this.get('fallbackRouteAfter'), model.get('id'));
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
willTransition: function(/*transition*/) {
|
||||
const controller = this.get('controller');
|
||||
const model = controller.get('model');
|
||||
|
||||
if (model.get('isNew')) {
|
||||
model.destroyRecord();
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
@ -4,6 +4,8 @@ import ajaxError from '../utils/ajax-error';
|
|||
const { Mixin } = Ember;
|
||||
|
||||
export default Mixin.create({
|
||||
fallbackRoute: null,
|
||||
|
||||
actions: {
|
||||
save: function() {
|
||||
const model = this.get('model');
|
||||
|
|
|
@ -1,29 +1,9 @@
|
|||
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 species = this.get('model');
|
||||
const { Controller } = Ember;
|
||||
|
||||
if (species.get('hasDirtyAttributes')) {
|
||||
species.save().then((species) => {
|
||||
this.transitionToRoute('protected.species.show', species.get('id'));
|
||||
}, () => {
|
||||
ajaxError(species.get('errors'), this.get('flashMessages'));
|
||||
});
|
||||
} else {
|
||||
species.destroyRecord().then(() => {
|
||||
this.transitionToRoute('protected.species.index');
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
cancel: function() {
|
||||
this.get('model').destroyRecord().then(() => {
|
||||
this.transitionToRoute('protected.species.index');
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
export default Controller.extend(SaveModel, {
|
||||
// Required for SaveModel mixin
|
||||
fallbackRoute: 'protected.species.show',
|
||||
});
|
||||
|
|
|
@ -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.species.index');
|
||||
}
|
||||
});
|
||||
},
|
||||
export default Route.extend(ElevatedAccess, {
|
||||
// Required for ElevatedAccess mixin
|
||||
fallbackRouteBefore: 'protected.species.index',
|
||||
fallbackRouteAfter: 'protected.species.show',
|
||||
|
||||
model: function() {
|
||||
return this.store.createRecord('species');
|
||||
},
|
||||
|
||||
actions: {
|
||||
willTransition: function(/*transition*/) {
|
||||
const controller = this.get('controller');
|
||||
const species = controller.get('model');
|
||||
|
||||
if (species.get('isNew')) {
|
||||
species.destroyRecord();
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
});
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{{
|
||||
protected/species/species-form
|
||||
species=model
|
||||
save="save"
|
||||
cancel="cancel"
|
||||
on-save=(action "save")
|
||||
on-cancel=(action "cancel")
|
||||
}}
|
||||
|
|
Reference in a new issue