From 66c185e441cdab1c20862d1f76c07548098f0d72 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 2 Dec 2015 10:13:44 -0700 Subject: [PATCH] Chained error handling in strains controller --- app/pods/protected/strains/edit/controller.js | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/pods/protected/strains/edit/controller.js b/app/pods/protected/strains/edit/controller.js index 9685884..d8aa4d9 100644 --- a/app/pods/protected/strains/edit/controller.js +++ b/app/pods/protected/strains/edit/controller.js @@ -1,9 +1,10 @@ import Ember from 'ember'; -import ajaxError from '../../../../utils/ajax-error'; -const { Controller, RSVP } = Ember; +const { Controller, RSVP, inject: { service } } = Ember; export default Controller.extend({ + ajaxError: service('ajax-error'), + fallbackRouteSave: 'protected.strains.show', fallbackRouteCancel: 'protected.strains.show', @@ -12,36 +13,31 @@ export default Controller.extend({ let promises = []; properties.measurements.forEach((measurement) => { if (measurement.get('isNew')) { - promises.push(measurement.save().catch(() => { - ajaxError(measurement.get('errors'), this.get('flashMessages')); - })); + promises.push(measurement.save()); } }); updateQueue.forEach((measurement) => { - promises.push(measurement.save().catch(() => { - ajaxError(measurement.get('errors'), this.get('flashMessages')); - })); + promises.push(measurement.save()); }); deleteQueue.forEach((measurement) => { - promises.push(measurement.destroyRecord().catch(() => { - ajaxError(measurement.get('errors'), this.get('flashMessages')); - })); + promises.push(measurement.destroyRecord()); }); const model = this.get('model'); const fallbackRoute = this.get('fallbackRouteSave'); RSVP.all(promises).then(() => { - // Can't call _super inside promise :-( + // Can't call _super inside promise, have to reproduce save-model + // mixin here :-( model.setProperties(properties); model.save().then((model) => { this.get('flashMessages').clearMessages(); this.transitionToRoute(fallbackRoute, model); - }, () => { - ajaxError(model.get('errors'), this.get('flashMessages')); }); + }, (errors) => { + this.get('ajaxError').alert(errors); }); },