From 233a2d09a16bf79bb8f0c88da8e45b3f881da754 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 1 Dec 2015 07:03:43 -0700 Subject: [PATCH] Refactoring delete measurement --- app/pods/protected/strains/edit/controller.js | 16 ++++++++-------- app/pods/protected/strains/edit/template.hbs | 1 - .../strains/strain-form/component.js | 19 ++++++++++++++----- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/pods/protected/strains/edit/controller.js b/app/pods/protected/strains/edit/controller.js index b3bf9b8..2591853 100644 --- a/app/pods/protected/strains/edit/controller.js +++ b/app/pods/protected/strains/edit/controller.js @@ -10,6 +10,14 @@ export default Controller.extend(SaveModel, { fallbackRouteCancel: 'protected.strains.show', actions: { + save: function(properties, deleteQueue) { + deleteQueue.forEach((val) => { + val.destroyRecord(); + }); + + this._super(properties); + }, + addCharacteristic: function() { return this.store.createRecord('measurement', { characteristic: this.store.createRecord('characteristic', { sortOrder: -999 }), @@ -25,13 +33,5 @@ export default Controller.extend(SaveModel, { }); }, - deleteMeasurement: function(measurement) { - const characteristic = measurement.get('characteristic'); - if (characteristic.get('isNew')) { - characteristic.destroyRecord(); - } - measurement.destroyRecord(); - }, - }, }); diff --git a/app/pods/protected/strains/edit/template.hbs b/app/pods/protected/strains/edit/template.hbs index 59d1633..f252090 100644 --- a/app/pods/protected/strains/edit/template.hbs +++ b/app/pods/protected/strains/edit/template.hbs @@ -5,7 +5,6 @@ add-characteristic=(action "addCharacteristic") allCharacteristics=allCharacteristics save-measurement=(action "saveMeasurement") - delete-measurement=(action "deleteMeasurement") on-save=(action "save") on-cancel=(action "cancel") }} diff --git a/app/pods/protected/strains/strain-form/component.js b/app/pods/protected/strains/strain-form/component.js index ebb157c..4b1518c 100644 --- a/app/pods/protected/strains/strain-form/component.js +++ b/app/pods/protected/strains/strain-form/component.js @@ -10,6 +10,7 @@ export default Component.extend(SetupMetaData, { isDirty: false, speciesList: null, allCharacteristics: null, + deleteQueue: [], // Actions "on-save": null, @@ -17,14 +18,13 @@ export default Component.extend(SetupMetaData, { "on-update": null, "add-characteristic": null, "save-measurement": null, - "delete-measurement": null, // CPs sortParams: ['sortOrder'], sortedSpeciesList: sort('speciesList', 'sortParams'), // Property mapping - propertiesList: ['strainName', 'typeStrain', 'species', 'isolatedFrom', 'accessionNumbers', 'genbank', 'wholeGenomeSequence', 'notes'], + propertiesList: ['strainName', 'typeStrain', 'species', 'isolatedFrom', 'accessionNumbers', 'genbank', 'wholeGenomeSequence', 'notes', 'measurements'], strainName: null, typeStrain: null, species: null, @@ -33,6 +33,7 @@ export default Component.extend(SetupMetaData, { genbank: null, wholeGenomeSequence: null, notes: null, + measurements: [], resetOnInit: Ember.on('init', function() { this.get('propertiesList').forEach((field) => { @@ -55,7 +56,7 @@ export default Component.extend(SetupMetaData, { actions: { save: function() { - return this.attrs['on-save'](this.getProperties(this.get('propertiesList'))); + return this.attrs['on-save'](this.getProperties(this.get('propertiesList')), this.get('deleteQueue')); }, cancel: function() { @@ -70,8 +71,16 @@ export default Component.extend(SetupMetaData, { return this.attrs['save-measurement'](measurement, properties); }, - deleteMeasurement: function(measurement) { - return this.attrs['delete-measurement'](measurement); + deleteMeasurement: function(value) { + const characteristic = value.get('characteristic'); + if (characteristic.get('isNew')) { + this.get('deleteQueue').pushObject(characteristic); + } + this.get('deleteQueue').pushObject(value); + + let measurements = this.get('measurements'); + measurements.removeObject(value); + this.set('isDirty', true); }, strainNameDidChange: function(value) {