Ember: set array value for specific key

不羁岁月 提交于 2019-12-13 04:17:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!