Refactor characteristics/new
This commit is contained in:
		
							parent
							
								
									7106696533
								
							
						
					
					
						commit
						9cb030ab02
					
				
					 7 changed files with 24 additions and 55 deletions
				
			
		|  | @ -4,12 +4,13 @@ import ajaxError from '../utils/ajax-error'; | ||||||
| const { Mixin } = Ember; | const { Mixin } = Ember; | ||||||
| 
 | 
 | ||||||
| export default Mixin.create({ | export default Mixin.create({ | ||||||
|   fallbackRoute: null, |   fallbackRouteSave: null, | ||||||
|  |   fallbackRouteCancel: null, | ||||||
| 
 | 
 | ||||||
|   actions: { |   actions: { | ||||||
|     save: function(properties) { |     save: function(properties) { | ||||||
|       const model = this.get('model'); |       const model = this.get('model'); | ||||||
|       const fallbackRoute = this.get('fallbackRoute'); |       const fallbackRoute = this.get('fallbackRouteSave'); | ||||||
| 
 | 
 | ||||||
|       model.setProperties(properties); |       model.setProperties(properties); | ||||||
| 
 | 
 | ||||||
|  | @ -30,7 +31,7 @@ export default Mixin.create({ | ||||||
|       model.get('errors').clear(); |       model.get('errors').clear(); | ||||||
|       model.rollbackAttributes(); |       model.rollbackAttributes(); | ||||||
| 
 | 
 | ||||||
|       this.transitionToRoute(this.get('fallbackRoute'), model); |       this.transitionToRoute(this.get('fallbackRouteCancel'), model); | ||||||
|     }, |     }, | ||||||
|   }, |   }, | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -5,5 +5,6 @@ const { Controller } = Ember; | ||||||
| 
 | 
 | ||||||
| export default Controller.extend(SaveModel, { | export default Controller.extend(SaveModel, { | ||||||
|   // Required for SaveModel mixin
 |   // Required for SaveModel mixin
 | ||||||
|   fallbackRoute: 'protected.characteristics.show', |   fallbackRouteSave: 'protected.characteristics.show', | ||||||
|  |   fallbackRouteCancel: 'protected.characteristics.index', | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -1,29 +1,10 @@ | ||||||
| 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 characteristic = this.get('model'); |  | ||||||
| 
 | 
 | ||||||
|       if (characteristic.get('hasDirtyAttributes')) { | export default Controller.extend(SaveModel, { | ||||||
|         characteristic.save().then((characteristic) => { |   // Required for SaveModel mixin
 | ||||||
|           this.transitionToRoute('protected.characteristics.show', characteristic); |   fallbackRouteSave: 'protected.characteristics.show', | ||||||
|         }, () => { |   fallbackRouteCancel: 'protected.characteristics.index', | ||||||
|           ajaxError(characteristic.get('errors'), this.get('flashMessages')); |  | ||||||
|         }); |  | ||||||
|       } else { |  | ||||||
|         characteristic.destroyRecord().then(() => { |  | ||||||
|           this.transitionToRoute('protected.characteristics.index'); |  | ||||||
|         }); |  | ||||||
|       } |  | ||||||
|     }, |  | ||||||
| 
 |  | ||||||
|     cancel: function() { |  | ||||||
|       this.get('model').destroyRecord().then(() => { |  | ||||||
|         this.transitionToRoute('protected.characteristics.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.characteristics.index', | ||||||
|       if (user.get('isReader')) { |   fallbackRouteAfter: 'protected.characteristics.show', | ||||||
|         this.transitionTo('protected.characteristics.index'); |  | ||||||
|       } |  | ||||||
|     }); |  | ||||||
|   }, |  | ||||||
| 
 | 
 | ||||||
|   model: function() { |   model: function() { | ||||||
|     return this.store.createRecord('characteristic'); |     return this.store.createRecord('characteristic'); | ||||||
|   }, |   }, | ||||||
| 
 |  | ||||||
|   actions: { |  | ||||||
|     willTransition: function(/*transition*/) { |  | ||||||
|       const controller = this.get('controller'); |  | ||||||
|       const characteristic = controller.get('model'); |  | ||||||
| 
 |  | ||||||
|       if (characteristic.get('isNew')) { |  | ||||||
|         characteristic.destroyRecord(); |  | ||||||
|       } |  | ||||||
|     }, |  | ||||||
|   }, |  | ||||||
| 
 |  | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| {{ | {{ | ||||||
|   protected/characteristics/characteristic-form |   protected/characteristics/characteristic-form | ||||||
|   characteristic=model |   characteristic=model | ||||||
|   save="save" |   on-save=(action "save") | ||||||
|   cancel="cancel" |   on-cancel=(action "cancel") | ||||||
| }} | }} | ||||||
|  |  | ||||||
|  | @ -5,5 +5,6 @@ const { Controller } = Ember; | ||||||
| 
 | 
 | ||||||
| export default Controller.extend(SaveModel, { | export default Controller.extend(SaveModel, { | ||||||
|   // Required for SaveModel mixin
 |   // Required for SaveModel mixin
 | ||||||
|   fallbackRoute: 'protected.species.show', |   fallbackRouteSave: 'protected.species.show', | ||||||
|  |   fallbackRouteCancel: 'protected.species.show', | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -5,5 +5,6 @@ const { Controller } = Ember; | ||||||
| 
 | 
 | ||||||
| export default Controller.extend(SaveModel, { | export default Controller.extend(SaveModel, { | ||||||
|   // Required for SaveModel mixin
 |   // Required for SaveModel mixin
 | ||||||
|   fallbackRoute: 'protected.species.show', |   fallbackRouteSave: 'protected.species.show', | ||||||
|  |   fallbackRouteCancel: 'protected.species.index', | ||||||
| }); | }); | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon