Wrapping up form cleanup

This commit is contained in:
Matthew Dillon 2015-11-04 12:26:43 -07:00
parent dbcc573f69
commit 30e7000a81
2 changed files with 38 additions and 15 deletions

View file

@ -5,23 +5,30 @@ const { Component, inject: { service } } = Ember;
export default Component.extend({ export default Component.extend({
currentUser: service('session-account'), currentUser: service('session-account'),
// Read-only attributes
species: null, species: null,
isNew: null,
isDirty: false,
// Actions
"on-save": null, "on-save": null,
"on-cancel": null, "on-cancel": null,
"on-update": null, "on-update": null,
// Property mapping
propertiesList: ['speciesName', 'typeSpecies', 'strains', 'etymology'],
speciesName: null, speciesName: null,
typeSpecies: null, typeSpecies: null,
strains: null,
updateField: function(property, value) { etymology: null,
this.set(property, value);
},
resetOnInit: Ember.on('init', function() { resetOnInit: Ember.on('init', function() {
['speciesName', 'typeSpecies'].forEach((field) => { this.get('propertiesList').forEach((field) => {
const valueInSpecies = this.get('species').get(field); const valueInSpecies = this.get('species').get(field);
this.set(field, valueInSpecies); this.set(field, valueInSpecies);
}); });
// Read-only attributes
this.set('isNew', this.get('species.isNew'));
}), }),
setupMetaDataOnInit: Ember.on('init', function() { setupMetaDataOnInit: Ember.on('init', function() {
@ -30,21 +37,35 @@ export default Component.extend({
}); });
}), }),
updateField: function(property, value) {
this.set(property, value);
// Manually compare against passed in value
if (this.get('species').get(property) !== value) {
this.set('isDirty', true);
} else {
this.set('isDirty', false);
}
},
actions: { actions: {
save: function() { save: function() {
return this.attrs['on-save'](this.getProperties(['speciesName', 'typeSpecies'])); return this.attrs['on-save'](this.getProperties(this.get('propertiesList')));
}, },
cancel: function() { cancel: function() {
return this.attrs['on-cancel'](); return this.attrs['on-cancel']();
}, },
nameDidChange: function(value) { speciesNameDidChange: function(value) {
this.updateField('speciesName', value); this.updateField('speciesName', value);
}, },
typeSpeciesDidChange: function() { typeSpeciesDidChange: function() {
this.toggleProperty('typeSpecies'); this.updateField('typeSpecies', !this.get('typeSpecies'));
},
etymologyDidChange: function(value) {
this.updateField('etymology', value);
}, },
}, },
}); });

View file

@ -4,7 +4,7 @@
<div data-row-span="2"> <div data-row-span="2">
<div data-field-span="1"> <div data-field-span="1">
<label>Species Name</label> <label>Species Name</label>
{{one-way-input type="text" class="species-name" value=speciesName update=(action "nameDidChange")}} {{one-way-input type="text" class="species-name" value=speciesName update=(action "speciesNameDidChange")}}
</div> </div>
<div data-field-span="1"> <div data-field-span="1">
<label>Type Species?</label> <label>Type Species?</label>
@ -12,11 +12,11 @@
{{if typeSpecies 'Yes' 'No'}} {{if typeSpecies 'Yes' 'No'}}
</div> </div>
</div> </div>
{{#unless species.isNew}} {{#unless isNew}}
<div data-row-span="1"> <div data-row-span="1">
<div data-field-span="1"> <div data-field-span="1">
<label>Strains</label> <label>Strains</label>
{{#each species.strains as |strain index|}} {{#each strains as |strain index|}}
{{if index ","}} {{if index ","}}
{{#link-to 'protected.strains.show' strain.id}} {{#link-to 'protected.strains.show' strain.id}}
{{{strain.strainNameMU}}} {{{strain.strainNameMU}}}
@ -30,7 +30,7 @@
<div data-row-span="1"> <div data-row-span="1">
<div data-field-span="1"> <div data-field-span="1">
<label>Etymology</label> <label>Etymology</label>
{{text-editor value=species.etymology}} {{text-editor value=etymology update=(action "etymologyDidChange")}}
</div> </div>
</div> </div>
</fieldset> </fieldset>
@ -38,7 +38,9 @@
<a class="button-red smaller" {{action 'cancel'}}> <a class="button-red smaller" {{action 'cancel'}}>
Cancel Cancel
</a> </a>
{{# if isDirty}}
<button type="submit" class="button-green smaller save-species"> <button type="submit" class="button-green smaller save-species">
Save Save
</button> </button>
{{/if}}
</form> </form>