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