Refactor strain form and edit strain
This commit is contained in:
parent
04486880a0
commit
29c507af6b
5 changed files with 122 additions and 69 deletions
|
@ -1,32 +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 strain = this.get('strain');
|
||||
const { Controller } = Ember;
|
||||
|
||||
if (strain.get('hasDirtyAttributes')) {
|
||||
strain.save().then((strain) => {
|
||||
this.transitionToRoute('protected.strains.show', strain);
|
||||
}, () => {
|
||||
ajaxError(strain.get('errors'), this.get('flashMessages'));
|
||||
});
|
||||
} else {
|
||||
strain.destroyRecord().then(() => {
|
||||
this.transitionToRoute('protected.strains.show', strain);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
cancel: function() {
|
||||
let strain = this.get('strain');
|
||||
|
||||
strain.get('errors').clear();
|
||||
strain.rollbackAttributes();
|
||||
|
||||
this.transitionToRoute('protected.strains.show', strain);
|
||||
},
|
||||
|
||||
},
|
||||
export default Controller.extend(SaveModel, {
|
||||
// Required for SaveModel mixin
|
||||
fallbackRouteSave: 'protected.strains.show',
|
||||
fallbackRouteCancel: 'protected.strains.show',
|
||||
});
|
||||
|
|
|
@ -1,35 +1,42 @@
|
|||
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.strains.index');
|
||||
}
|
||||
});
|
||||
},
|
||||
export default Route.extend(ElevatedAccess, {
|
||||
// Required for ElevatedAccess mixin
|
||||
fallbackRouteBefore: 'protected.strains.index',
|
||||
fallbackRouteAfter: 'protected.strains.show',
|
||||
|
||||
model: function(params) {
|
||||
return Ember.RSVP.hash({
|
||||
strain: this.store.find('strain', params.strain_id),
|
||||
strain: this.store.findRecord('strain', params.strain_id),
|
||||
species: this.store.findAll('species'), // Need for dropdown
|
||||
});
|
||||
},
|
||||
|
||||
// Overriding afterModel because of RSVP hash
|
||||
afterModel: function(models) {
|
||||
if (!models.strain.get('canEdit')) {
|
||||
this.transitionTo('strains.show', models.strain.get('id'));
|
||||
if (!models.strain.get('isNew') && !models.strain.get('canEdit')) {
|
||||
this.transitionTo(this.get('fallbackRouteAfter'), models.strain.get('id'));
|
||||
}
|
||||
},
|
||||
|
||||
// Setting up controller because of RSVP hash
|
||||
setupController: function(controller, models) {
|
||||
controller.setProperties(models);
|
||||
this.get('currentUser.account').then((user) => {
|
||||
controller.set('metaData', user.get('metaData'));
|
||||
});
|
||||
controller.set('model', models.strain);
|
||||
controller.set('speciesList', models.species);
|
||||
},
|
||||
|
||||
actions: {
|
||||
// Overriding willTransition because of RSVP hash
|
||||
willTransition: function(/*transition*/) {
|
||||
const controller = this.get('controller');
|
||||
const model = controller.get('model');
|
||||
|
||||
if (model.get('isNew')) {
|
||||
model.destroyRecord();
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{{
|
||||
protected/strains/strain-form
|
||||
strain=strain
|
||||
species=species
|
||||
strain=model
|
||||
speciesList=speciesList
|
||||
canAdd=metaData.canAdd
|
||||
save="save"
|
||||
cancel="cancel"
|
||||
on-save=(action "save")
|
||||
on-cancel=(action "cancel")
|
||||
}}
|
||||
|
|
Reference in a new issue