diff --git a/app/pods/protected/strains/edit/controller.js b/app/pods/protected/strains/edit/controller.js index 17f47d6..4d68665 100644 --- a/app/pods/protected/strains/edit/controller.js +++ b/app/pods/protected/strains/edit/controller.js @@ -16,7 +16,8 @@ export default Controller.extend(SaveModel, { }); }, - saveMeasurement: function(measurement) { + saveMeasurement: function(measurement, properties) { + measurement.setProperties(properties); measurement.save().then(() => { this.get('flashMessages').clearMessages(); }, () => { diff --git a/app/pods/protected/strains/measurements-table-row/component.js b/app/pods/protected/strains/measurements-table-row/component.js index cba52a6..b818de6 100644 --- a/app/pods/protected/strains/measurements-table-row/component.js +++ b/app/pods/protected/strains/measurements-table-row/component.js @@ -4,31 +4,47 @@ const { Component, computed } = Ember; export default Component.extend({ tagName: 'tr', + + // Read-only attributes isEditing: false, allCharacteristics: null, measurement: null, + isDirty: null, // Actions "save-measurement": null, "delete-measurement": null, - oldCharacteristicId: function() { - const json = this.get('measurement').toJSON(); - return json.characteristic; - }.property(), + // Property mapping + propertiesList: ['characteristic', 'value', 'notes'], + characteristic: null, + value: null, + notes: null, - rowChanged: computed('measurement.notes', 'measurement.value', 'measurement.characteristic.id', function() { - return this.get('measurement.hasDirtyAttributes') || - this.get('oldCharacteristicId') !== this.get('measurement.characteristic.id'); + resetOnInit: Ember.on('init', function() { + this.get('propertiesList').forEach((field) => { + const valueInMeasurement = this.get('measurement').get(field); + this.set(field, valueInMeasurement); + }); }), + updateField: function(property, value) { + this.set(property, value); + // Manually compare against passed in value + if (this.get('measurement').get(property) !== value) { + this.set('isDirty', true); + } else { + this.set('isDirty', false); + } + }, + actions: { edit: function() { this.toggleProperty('isEditing'); }, save: function() { - this.attrs['save-measurement'](this.get('measurement')); + this.attrs['save-measurement'](this.get('measurement'), this.getProperties(this.get('propertiesList'))); this.toggleProperty('isEditing'); }, @@ -36,5 +52,17 @@ export default Component.extend({ this.attrs['delete-measurement'](this.get('measurement')); }, + characteristicDidChange: function(value) { + this.updateField('characteristic', value); + }, + + valueDidChange: function(value) { + this.updateField('value', value); + }, + + notesDidChange: function(value) { + this.updateField('notes', value); + }, + }, }); diff --git a/app/pods/protected/strains/measurements-table-row/template.hbs b/app/pods/protected/strains/measurements-table-row/template.hbs index cab5554..5bb44f0 100644 --- a/app/pods/protected/strains/measurements-table-row/template.hbs +++ b/app/pods/protected/strains/measurements-table-row/template.hbs @@ -5,19 +5,19 @@ select-2 multiple=false content=allCharacteristics - value=measurement.characteristic + value=characteristic optionLabelPath="characteristicName" }} - {{input value=measurement.value}} + {{one-way-input type="text" class="measurement-value" value=value update=(action "valueDidChange")}} - {{input value=measurement.notes}} + {{one-way-input type="text" class="measurement-notes" value=notes update=(action "notesDidChange")}} {{#if canEdit}} - {{#if rowChanged}} + {{#if isDirty}} diff --git a/app/pods/protected/strains/measurements-table/component.js b/app/pods/protected/strains/measurements-table/component.js index ab0b72e..e4fd3a1 100644 --- a/app/pods/protected/strains/measurements-table/component.js +++ b/app/pods/protected/strains/measurements-table/component.js @@ -43,8 +43,8 @@ export default Component.extend({ this.set('sortAsc', true); }, - saveMeasurement: function(measurement) { - return this.attrs['save-measurement'](measurement); + saveMeasurement: function(measurement, properties) { + return this.attrs['save-measurement'](measurement, properties); }, deleteMeasurement: function(measurement) { diff --git a/app/pods/protected/strains/strain-form/component.js b/app/pods/protected/strains/strain-form/component.js index be823bc..420db5f 100644 --- a/app/pods/protected/strains/strain-form/component.js +++ b/app/pods/protected/strains/strain-form/component.js @@ -62,8 +62,8 @@ export default Component.extend(SetupMetaData, { return this.attrs['add-characteristic'](); }, - saveMeasurement: function(measurement) { - return this.attrs['save-measurement'](measurement); + saveMeasurement: function(measurement, properties) { + return this.attrs['save-measurement'](measurement, properties); }, deleteMeasurement: function(measurement) {