Refactor strain form and edit strain

This commit is contained in:
Matthew Dillon 2015-11-10 12:57:12 -07:00
parent 04486880a0
commit 29c507af6b
5 changed files with 122 additions and 69 deletions

View file

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