Chained error handling in strains controller
This commit is contained in:
parent
df5a50b1e2
commit
66c185e441
1 changed files with 10 additions and 14 deletions
|
@ -1,9 +1,10 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
import ajaxError from '../../../../utils/ajax-error';
|
|
||||||
|
|
||||||
const { Controller, RSVP } = Ember;
|
const { Controller, RSVP, inject: { service } } = Ember;
|
||||||
|
|
||||||
export default Controller.extend({
|
export default Controller.extend({
|
||||||
|
ajaxError: service('ajax-error'),
|
||||||
|
|
||||||
fallbackRouteSave: 'protected.strains.show',
|
fallbackRouteSave: 'protected.strains.show',
|
||||||
fallbackRouteCancel: 'protected.strains.show',
|
fallbackRouteCancel: 'protected.strains.show',
|
||||||
|
|
||||||
|
@ -12,36 +13,31 @@ export default Controller.extend({
|
||||||
let promises = [];
|
let promises = [];
|
||||||
properties.measurements.forEach((measurement) => {
|
properties.measurements.forEach((measurement) => {
|
||||||
if (measurement.get('isNew')) {
|
if (measurement.get('isNew')) {
|
||||||
promises.push(measurement.save().catch(() => {
|
promises.push(measurement.save());
|
||||||
ajaxError(measurement.get('errors'), this.get('flashMessages'));
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
updateQueue.forEach((measurement) => {
|
updateQueue.forEach((measurement) => {
|
||||||
promises.push(measurement.save().catch(() => {
|
promises.push(measurement.save());
|
||||||
ajaxError(measurement.get('errors'), this.get('flashMessages'));
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
deleteQueue.forEach((measurement) => {
|
deleteQueue.forEach((measurement) => {
|
||||||
promises.push(measurement.destroyRecord().catch(() => {
|
promises.push(measurement.destroyRecord());
|
||||||
ajaxError(measurement.get('errors'), this.get('flashMessages'));
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const model = this.get('model');
|
const model = this.get('model');
|
||||||
const fallbackRoute = this.get('fallbackRouteSave');
|
const fallbackRoute = this.get('fallbackRouteSave');
|
||||||
|
|
||||||
RSVP.all(promises).then(() => {
|
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.setProperties(properties);
|
||||||
model.save().then((model) => {
|
model.save().then((model) => {
|
||||||
this.get('flashMessages').clearMessages();
|
this.get('flashMessages').clearMessages();
|
||||||
this.transitionToRoute(fallbackRoute, model);
|
this.transitionToRoute(fallbackRoute, model);
|
||||||
}, () => {
|
|
||||||
ajaxError(model.get('errors'), this.get('flashMessages'));
|
|
||||||
});
|
});
|
||||||
|
}, (errors) => {
|
||||||
|
this.get('ajaxError').alert(errors);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Reference in a new issue