问题
How do I set a value for a specific array key in Ember?
I tried ...
var feed = this.get('controller.feed');
feed[i]['loadingFeedImage'] = false;
this.set('controller.feed', feed);
... but the template doesn't recognize the changed value.
I also tried this, but it doesn't work:
this.get('controller.feed['+ i +'].loadingFeedImage', false);
回答1:
I figured it out:
Change the value like this: this.set('controller.feed.data.' + i +'.loadingFeedImage', false); (don't use square brackets).
回答2:
JavaScript objects are reference based. So editing something in an array using it's reference updates triggers an observer update.
The reason your first solution does not work is because you don't use set at the second line.
Using ember array methods:
// assuming the array is an Ember.Array
this.get('controller.feed').objectAt(i).set('loadingFeedImage', false)
Make sure your array is an Ember.Array
Your provided solution uses the private api. I would advise you not to use it.
来源:https://stackoverflow.com/questions/24905879/ember-set-array-value-for-specific-key