Edit characteristic

This commit is contained in:
Matthew Dillon 2015-09-04 08:45:54 -07:00
parent 24c870353f
commit f25fae0a83
6 changed files with 65 additions and 6 deletions

View file

@ -12,4 +12,5 @@ export default DS.Model.extend({
updatedBy : DS.attr('number'), updatedBy : DS.attr('number'),
deletedBy : DS.attr('number'), deletedBy : DS.attr('number'),
sortOrder : DS.attr('number'), sortOrder : DS.attr('number'),
canEdit : DS.attr('boolean'),
}); });

View file

@ -1,6 +1,6 @@
import Ember from 'ember'; import Ember from 'ember';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
sortParams: ['characteristicTypeName', 'sortOrder'], sortParams: ['characteristicTypeName', 'sortOrder', 'characteristicName'],
sortedCharacteristics: Ember.computed.sort('model', 'sortParams'), sortedCharacteristics: Ember.computed.sort('model', 'sortParams'),
}); });

View file

@ -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');
},
}
});

View file

@ -0,0 +1,15 @@
{{#if isEditing}}
<td>{{input value=characteristicName}}</td>
<td>{{input value=characteristicTypeName}}</td>
<td>{{input value=sortOrder}}</td>
<td {{action 'save'}}>Save</td>
{{else}}
<td>{{row.characteristicName}}</td>
<td>{{row.characteristicTypeName}}</td>
<td>{{row.sortOrder}}</td>
{{#if row.canEdit}}
<td {{action 'edit'}}>Edit</td>
{{else}}
<td></td>
{{/if}}
{{/if}}

View file

@ -7,15 +7,12 @@
<th>Name</th> <th>Name</th>
<th>Type</th> <th>Type</th>
<th>Sort Order</th> <th>Sort Order</th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{#each sortedCharacteristics as |row|}} {{#each sortedCharacteristics as |row|}}
<tr> {{protected/characteristics/editable-row row=row}}
<td>{{row.characteristicName}}</td>
<td>{{row.characteristicTypeName}}</td>
<td>{{row.sortOrder}}</td>
</tr>
{{/each}} {{/each}}
</tbody> </tbody>
</table> </table>

View file

@ -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'));
});
}
});