Queue new measurements
Still some bound attributes / coupling, but getting better
This commit is contained in:
parent
14698d0394
commit
2811143066
7 changed files with 101 additions and 39 deletions
|
@ -1,21 +1,61 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
import SaveModel from '../../../../mixins/save-model';
|
|
||||||
import ajaxError from '../../../../utils/ajax-error';
|
import ajaxError from '../../../../utils/ajax-error';
|
||||||
|
|
||||||
const { Controller } = Ember;
|
const { Controller, RSVP } = Ember;
|
||||||
|
|
||||||
export default Controller.extend(SaveModel, {
|
export default Controller.extend({
|
||||||
// Required for SaveModel mixin
|
|
||||||
fallbackRouteSave: 'protected.strains.show',
|
fallbackRouteSave: 'protected.strains.show',
|
||||||
fallbackRouteCancel: 'protected.strains.show',
|
fallbackRouteCancel: 'protected.strains.show',
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
save: function(properties, deleteQueue) {
|
save: function(properties, deleteQueue, updateQueue) {
|
||||||
deleteQueue.forEach((val) => {
|
let promises = [];
|
||||||
val.destroyRecord();
|
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() {
|
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'));
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
speciesList=speciesList
|
speciesList=speciesList
|
||||||
add-characteristic=(action "addCharacteristic")
|
add-characteristic=(action "addCharacteristic")
|
||||||
allCharacteristics=allCharacteristics
|
allCharacteristics=allCharacteristics
|
||||||
save-measurement=(action "saveMeasurement")
|
|
||||||
on-save=(action "save")
|
on-save=(action "save")
|
||||||
on-cancel=(action "cancel")
|
on-cancel=(action "cancel")
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -11,6 +11,7 @@ export default Component.extend({
|
||||||
measurement: null,
|
measurement: null,
|
||||||
isDirty: null,
|
isDirty: null,
|
||||||
isNew: false,
|
isNew: false,
|
||||||
|
isQueued: false,
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
"save-measurement": null,
|
"save-measurement": null,
|
||||||
|
@ -33,9 +34,12 @@ export default Component.extend({
|
||||||
});
|
});
|
||||||
// Read-only attributes
|
// Read-only attributes
|
||||||
this.set('isNew', this.get('measurement.isNew'));
|
this.set('isNew', this.get('measurement.isNew'));
|
||||||
if (this.get('isNew')) {
|
if (this.get('isNew') && !this.get('isQueued')) {
|
||||||
this.set('isEditing', true);
|
this.set('isEditing', true);
|
||||||
|
} else {
|
||||||
|
this.set('isEditing', false);
|
||||||
}
|
}
|
||||||
|
this.set('isDirty', false);
|
||||||
},
|
},
|
||||||
|
|
||||||
updateField: function(property, value) {
|
updateField: function(property, value) {
|
||||||
|
@ -54,10 +58,9 @@ export default Component.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
save: function() {
|
save: function() {
|
||||||
this.attrs['save-measurement'](this.get('measurement'), this.getProperties(this.get('propertiesList'))).then(() => {
|
this.attrs['save-measurement'](this.get('measurement'), this.getProperties(this.get('propertiesList')));
|
||||||
this._resetProperties();
|
this.set('isQueued', true);
|
||||||
this.set('isEditing', false);
|
this._resetProperties();
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
cancel: function() {
|
cancel: function() {
|
||||||
|
|
|
@ -5,7 +5,7 @@ const { sort } = computed;
|
||||||
|
|
||||||
export default Component.extend({
|
export default Component.extend({
|
||||||
// Passed in
|
// Passed in
|
||||||
strain: null,
|
measurements: null,
|
||||||
allCharacteristics: null,
|
allCharacteristics: null,
|
||||||
canEdit: false,
|
canEdit: false,
|
||||||
canAdd: false,
|
canAdd: false,
|
||||||
|
@ -19,15 +19,15 @@ export default Component.extend({
|
||||||
sortParams: ['characteristic.characteristicTypeName', 'characteristic.sortOrder', 'characteristic.characteristicName'],
|
sortParams: ['characteristic.characteristicTypeName', 'characteristic.sortOrder', 'characteristic.characteristicName'],
|
||||||
sortAsc: true,
|
sortAsc: true,
|
||||||
paramsChanged: false,
|
paramsChanged: false,
|
||||||
sortedMeasurements: sort('strain.measurements', 'sortParams'),
|
sortedMeasurements: sort('measurements', 'sortParams'),
|
||||||
measurementsPresent: computed('strain.measurements', function() {
|
measurementsPresent: computed('measurements', function() {
|
||||||
return this.get('strain.measurements.length') > 0;
|
return this.get('measurements.length') > 0;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
addCharacteristic: function() {
|
addCharacteristic: function() {
|
||||||
const newChar = this.attrs['add-characteristic']();
|
const measurement = this.attrs['add-characteristic']();
|
||||||
this.get('strain.measurements').addObject(newChar);
|
this.get('measurements').addObject(measurement);
|
||||||
},
|
},
|
||||||
|
|
||||||
changeSortParam: function(col) {
|
changeSortParam: function(col) {
|
||||||
|
|
|
@ -75,7 +75,7 @@
|
||||||
<dd>
|
<dd>
|
||||||
{{
|
{{
|
||||||
protected/strains/measurements-table
|
protected/strains/measurements-table
|
||||||
strain=strain
|
measurements=strain.measurements
|
||||||
canEdit=false
|
canEdit=false
|
||||||
canAdd=false
|
canAdd=false
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -10,6 +10,7 @@ export default Component.extend(SetupMetaData, {
|
||||||
isDirty: false,
|
isDirty: false,
|
||||||
speciesList: null,
|
speciesList: null,
|
||||||
allCharacteristics: null,
|
allCharacteristics: null,
|
||||||
|
updateQueue: [],
|
||||||
deleteQueue: [],
|
deleteQueue: [],
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
@ -17,7 +18,6 @@ export default Component.extend(SetupMetaData, {
|
||||||
"on-cancel": null,
|
"on-cancel": null,
|
||||||
"on-update": null,
|
"on-update": null,
|
||||||
"add-characteristic": null,
|
"add-characteristic": null,
|
||||||
"save-measurement": null,
|
|
||||||
|
|
||||||
// CPs
|
// CPs
|
||||||
sortParams: ['sortOrder'],
|
sortParams: ['sortOrder'],
|
||||||
|
@ -36,13 +36,38 @@ export default Component.extend(SetupMetaData, {
|
||||||
measurements: [],
|
measurements: [],
|
||||||
|
|
||||||
resetOnInit: Ember.on('init', function() {
|
resetOnInit: Ember.on('init', function() {
|
||||||
|
this._resetProperties();
|
||||||
|
}),
|
||||||
|
|
||||||
|
_resetProperties: function() {
|
||||||
|
// Still some coupling going on here because of adding strain to measurement
|
||||||
|
this.get('measurements').forEach((val) => {
|
||||||
|
if (val.get('hasDirtyAttributes')) {
|
||||||
|
val.rollbackAttributes();
|
||||||
|
}
|
||||||
|
if (val.get('isNew')) {
|
||||||
|
this.get('strain.measurements').removeObject(val);
|
||||||
|
}
|
||||||
|
});
|
||||||
this.get('propertiesList').forEach((field) => {
|
this.get('propertiesList').forEach((field) => {
|
||||||
const valueInStrain = this.get('strain').get(field);
|
const valueInStrain = this.get('strain').get(field);
|
||||||
this.set(field, valueInStrain);
|
if (field === 'measurements') {
|
||||||
|
let tempArray = [];
|
||||||
|
valueInStrain.forEach((val) => {
|
||||||
|
if (!val.get('isNew')) {
|
||||||
|
tempArray.push(val);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.set(field, tempArray);
|
||||||
|
} else {
|
||||||
|
this.set(field, valueInStrain);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
this.set('updateQueue', []);
|
||||||
|
this.set('deleteQueue', []);
|
||||||
// Read-only attributes
|
// Read-only attributes
|
||||||
this.set('isNew', this.get('strain.isNew'));
|
this.set('isNew', this.get('strain.isNew'));
|
||||||
}),
|
},
|
||||||
|
|
||||||
updateField: function(property, value) {
|
updateField: function(property, value) {
|
||||||
this.set(property, value);
|
this.set(property, value);
|
||||||
|
@ -56,10 +81,11 @@ export default Component.extend(SetupMetaData, {
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
save: function() {
|
save: function() {
|
||||||
return this.attrs['on-save'](this.getProperties(this.get('propertiesList')), this.get('deleteQueue'));
|
return this.attrs['on-save'](this.getProperties(this.get('propertiesList')), this.get('deleteQueue'), this.get('updateQueue'));
|
||||||
},
|
},
|
||||||
|
|
||||||
cancel: function() {
|
cancel: function() {
|
||||||
|
this._resetProperties();
|
||||||
return this.attrs['on-cancel']();
|
return this.attrs['on-cancel']();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -68,7 +94,12 @@ export default Component.extend(SetupMetaData, {
|
||||||
},
|
},
|
||||||
|
|
||||||
saveMeasurement: function(measurement, properties) {
|
saveMeasurement: function(measurement, properties) {
|
||||||
return this.attrs['save-measurement'](measurement, properties);
|
measurement.setProperties(properties);
|
||||||
|
measurement.set('strain', this.get('strain'));
|
||||||
|
if (!measurement.get('isNew')) {
|
||||||
|
this.get('updateQueue').pushObject(measurement);
|
||||||
|
}
|
||||||
|
this.set('isDirty', true);
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteMeasurement: function(value) {
|
deleteMeasurement: function(value) {
|
||||||
|
@ -77,9 +108,7 @@ export default Component.extend(SetupMetaData, {
|
||||||
this.get('deleteQueue').pushObject(characteristic);
|
this.get('deleteQueue').pushObject(characteristic);
|
||||||
}
|
}
|
||||||
this.get('deleteQueue').pushObject(value);
|
this.get('deleteQueue').pushObject(value);
|
||||||
|
this.get('measurements').removeObject(value);
|
||||||
let measurements = this.get('measurements');
|
|
||||||
measurements.removeObject(value);
|
|
||||||
this.set('isDirty', true);
|
this.set('isDirty', true);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@
|
||||||
<div>
|
<div>
|
||||||
{{
|
{{
|
||||||
protected/strains/measurements-table
|
protected/strains/measurements-table
|
||||||
strain=strain
|
measurements=measurements
|
||||||
add-characteristic=(action "addCharacteristic")
|
add-characteristic=(action "addCharacteristic")
|
||||||
allCharacteristics=allCharacteristics
|
allCharacteristics=allCharacteristics
|
||||||
save-measurement=(action "saveMeasurement")
|
save-measurement=(action "saveMeasurement")
|
||||||
|
|
Reference in a new issue