Reputation: 1597
I am trying to set a property on an input bound to a model, but I don't know how to get to this single element. Here is my controller action:
acceptEdit: function(item) {
items = this.get('items')
items.toArray().forEach(function(item){
item.set('isEditing', false);
});
items.save();
}
The Items belong to a Category. Here is the Category model:
App.Category = DS.Model.extend({
title: DS.attr('string'),
items: DS.hasMany('item'),
itemCount: function() {
return this.get('items.length');
}.property('items.@each')
});
So, when acceptEdit is called I can get all the items for the category for the current route, but I can't figure out how to get at the exact item being edited. Thus, my inelegant code that just loops over all items to set 'isEditing' to false. Clobbering works, but isn't ideal :\
Is there a way I can get the individual item?
Upvotes: 0
Views: 33
Reputation: 37389
You really only have two options: search through the array, or store the one that is currently being edited. If you don't want to store the one being edited, just use the findBy method to get the object that is being edited.
var items = this.get('items');
var editingItem = items.findBy('isEditing', true);
// I use tryInvoke in case one isn't found. That might not be possible for you.
Ember.tryInvoke(editingItem, 'set', ['isEditing', false]);
Upvotes: 3