From f25fae0a83345dae16e0cf5e7120d2ef84a0c8cd Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Fri, 4 Sep 2015 08:45:54 -0700 Subject: [PATCH] Edit characteristic --- app/models/characteristic.js | 1 + .../protected/characteristics/controller.js | 2 +- .../characteristics/editable-row/component.js | 28 +++++++++++++++++++ .../characteristics/editable-row/template.hbs | 15 ++++++++++ .../protected/characteristics/template.hbs | 7 ++--- app/serializers/characteristic.js | 18 ++++++++++++ 6 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 app/pods/protected/characteristics/editable-row/component.js create mode 100644 app/pods/protected/characteristics/editable-row/template.hbs create mode 100644 app/serializers/characteristic.js diff --git a/app/models/characteristic.js b/app/models/characteristic.js index d896ca1..ca4c5d9 100644 --- a/app/models/characteristic.js +++ b/app/models/characteristic.js @@ -12,4 +12,5 @@ export default DS.Model.extend({ updatedBy : DS.attr('number'), deletedBy : DS.attr('number'), sortOrder : DS.attr('number'), + canEdit : DS.attr('boolean'), }); diff --git a/app/pods/protected/characteristics/controller.js b/app/pods/protected/characteristics/controller.js index be9f042..0403cc5 100644 --- a/app/pods/protected/characteristics/controller.js +++ b/app/pods/protected/characteristics/controller.js @@ -1,6 +1,6 @@ import Ember from 'ember'; export default Ember.Controller.extend({ - sortParams: ['characteristicTypeName', 'sortOrder'], + sortParams: ['characteristicTypeName', 'sortOrder', 'characteristicName'], sortedCharacteristics: Ember.computed.sort('model', 'sortParams'), }); diff --git a/app/pods/protected/characteristics/editable-row/component.js b/app/pods/protected/characteristics/editable-row/component.js new file mode 100644 index 0000000..384d254 --- /dev/null +++ b/app/pods/protected/characteristics/editable-row/component.js @@ -0,0 +1,28 @@ +import Ember from 'ember'; + +export default Ember.Component.extend({ + tagName: 'tr', + + actions: { + edit: function() { + this.set('characteristicName', this.get('row.characteristicName')); + this.set('characteristicTypeName', this.get('row.characteristicTypeName')); + this.set('sortOrder', this.get('row.sortOrder')); + this.toggleProperty('isEditing'); + }, + + save: function() { + if (this.get('characteristicName') !== this.get('row.characteristicName') || + this.get('characteristicTypeName') !== this.get('row.characteristicTypeName') || + this.get('sortOrder') !== this.get('row.sortOrder')) { + this.set('row.characteristicName', this.get('characteristicName')); + this.set('row.characteristicTypeName', this.get('characteristicTypeName')); + this.set('row.sortOrder', this.get('sortOrder')); + this.get('row').save(); + } + this.toggleProperty('isEditing'); + }, + + } + +}); diff --git a/app/pods/protected/characteristics/editable-row/template.hbs b/app/pods/protected/characteristics/editable-row/template.hbs new file mode 100644 index 0000000..8230297 --- /dev/null +++ b/app/pods/protected/characteristics/editable-row/template.hbs @@ -0,0 +1,15 @@ +{{#if isEditing}} + {{input value=characteristicName}} + {{input value=characteristicTypeName}} + {{input value=sortOrder}} + Save +{{else}} + {{row.characteristicName}} + {{row.characteristicTypeName}} + {{row.sortOrder}} + {{#if row.canEdit}} + Edit + {{else}} + + {{/if}} +{{/if}} diff --git a/app/pods/protected/characteristics/template.hbs b/app/pods/protected/characteristics/template.hbs index a599cbd..12ce1c1 100644 --- a/app/pods/protected/characteristics/template.hbs +++ b/app/pods/protected/characteristics/template.hbs @@ -7,15 +7,12 @@ Name Type Sort Order + {{#each sortedCharacteristics as |row|}} - - {{row.characteristicName}} - {{row.characteristicTypeName}} - {{row.sortOrder}} - + {{protected/characteristics/editable-row row=row}} {{/each}} diff --git a/app/serializers/characteristic.js b/app/serializers/characteristic.js new file mode 100644 index 0000000..41ce180 --- /dev/null +++ b/app/serializers/characteristic.js @@ -0,0 +1,18 @@ +import DS from 'ember-data'; +import Ember from 'ember'; + +export default DS.RESTSerializer.extend({ + isNewSerializerAPI: true, + + serializeHasMany: function(snapshot, json, relationship) { + var key = relationship.key; + var hasMany = snapshot.hasMany(key); + key = this.keyForRelationship ? this.keyForRelationship(key, "hasMany", "serialize") : key; + + json[key] = []; + hasMany.forEach((item) => { + json[key].push(+item.get('id')); + }); + } + +});