[英]Unable to observe an array of objects in Ember.js?
I have an array of objects within a component like this: 我在这样的组件中有一组对象:
checkboxes: [
{
label: 'A',
state: false
},
{
label: 'B',
state: false
},
{
label: 'C',
state: false
}
]
In my Handlebars template, I have a loop to show the list of checkboxes and each checkbox state maps back to the array item like this: 在我的Handlebars模板中,我有一个循环来显示复选框列表,每个复选框状态都映射回数组项,如下所示:
{{#each checkboxes as |item|}}
<li><input type="checkbox" checked={{item.state}}> {{item.label}}</li>
{{/each}}
I wrote an observer to observe changes to the array but it doesn't seem to work: 我写了一个观察者来观察数组的变化,但是它似乎不起作用:
observeCheckboxes: function() {
console.log(this.get('checkboxes').toArray());
}.observes('checkboxes.[]')
How do I observe changes to the checkbox states so I can get a list of checked items? 如何观察复选框状态的变化,以便获得已检查项目的列表?
Change your observer to observe checkboxes.@each.state
. 更改您的观察者以遵守
checkboxes.@each.state
。
Have a look through the documentation here for a complete overview. 浏览此处的文档以获取完整概述。
If possible, try to avoid using an observer, and rather use a computed property, and then also to rather define computed properties and observers as Ember.computed & Ember.observer. 如果可能,请尝试避免使用观察者,而是使用计算的属性,然后将计算的属性和观察者定义为Ember.computed和Ember.observer。
Here is a twiddle that demonstrates why your observer is not firing. 这是一个旋转现象 ,展示了为什么观察者没有开火。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.