Queue new measurements

Still some bound attributes / coupling, but getting better
This commit is contained in:
Matthew Dillon 2015-12-01 13:13:24 -07:00
parent 14698d0394
commit 2811143066
7 changed files with 101 additions and 39 deletions

View file

@ -1,21 +1,61 @@
import Ember from 'ember';
import SaveModel from '../../../../mixins/save-model';
import ajaxError from '../../../../utils/ajax-error';
const { Controller } = Ember;
const { Controller, RSVP } = Ember;
export default Controller.extend(SaveModel, {
// Required for SaveModel mixin
export default Controller.extend({
fallbackRouteSave: 'protected.strains.show',
fallbackRouteCancel: 'protected.strains.show',
actions: {
save: function(properties, deleteQueue) {
deleteQueue.forEach((val) => {
val.destroyRecord();
save: function(properties, deleteQueue, updateQueue) {
let promises = [];
properties.measurements.forEach((measurement) => {
if (measurement.get('isNew')) {
promises.push(measurement.save().catch(() => {
ajaxError(measurement.get('errors'), this.get('flashMessages'));
}));
}
});
this._super(properties);
updateQueue.forEach((measurement) => {
promises.push(measurement.save().catch(() => {
ajaxError(measurement.get('errors'), this.get('flashMessages'));
}));
});
deleteQueue.forEach((measurement) => {
promises.push(measurement.destroyRecord().catch(() => {
ajaxError(measurement.get('errors'), this.get('flashMessages'));
}));
});
const model = this.get('model');
const fallbackRoute = this.get('fallbackRouteSave');
RSVP.all(promises).then(() => {
// Can't call _super inside promise :-(
model.setProperties(properties);
model.save().then((model) => {
this.get('flashMessages').clearMessages();
this.transitionToRoute(fallbackRoute, model);
}, () => {
ajaxError(model.get('errors'), this.get('flashMessages'));
});
});
},
cancel: function() {
const model = this.get('model');
model.get('errors').clear();
model.rollbackAttributes();
if (model.get('isNew')) {
this.transitionToRoute(this.get('fallbackRouteCancel'));
} else {
this.transitionToRoute(this.get('fallbackRouteCancel'), model);
}
},
addCharacteristic: function() {
@ -24,14 +64,5 @@ export default Controller.extend(SaveModel, {
});
},
saveMeasurement: function(measurement, properties) {
measurement.setProperties(properties);
return measurement.save().then(() => {
this.get('flashMessages').clearMessages();
}, () => {
ajaxError(measurement.get('errors'), this.get('flashMessages'));
});
},
},
});

View file

@ -4,7 +4,6 @@
speciesList=speciesList
add-characteristic=(action "addCharacteristic")
allCharacteristics=allCharacteristics
save-measurement=(action "saveMeasurement")
on-save=(action "save")
on-cancel=(action "cancel")
}}