我正在尝试修改 Todos 示例以更好地理解框架。
我正在尝试修改 todosController 以添加一个“已完成”计算属性,该属性返回所有已完成的 todos。最重要的是,我正在尝试更新“areAllCompleted”属性。
我有这段代码,当“完成”发生变化时,它不会更新“areAllCompleted”。
TodosThree.todosController = SC.ArrayController.create({
completed: function(){
if(this.get('content')){
return this.get('content').find(
SC.Query.local(
TodosThree.Todo,
'isCompleted = true'
)
);
}
else {
return [];
}
}.property('content').cacheable(),
areAllCompleted: function (k, v) {
console.log('get');
if (v !== undefined) {
this.setEach('isCompleted', v);
}
return this.getPath('completed.length') === this.get('length');
# This .property definition doesn't work with .*completed.length .. ?
}.property('length','.*completed.length')
});
但是,如果我稍微更改代码以添加绑定,它会起作用:
TodosThree.todosController = SC.ArrayController.create({
completed: function(){
if(this.get('content')){
return this.get('content').find(
SC.Query.local(
TodosThree.Todo,
'isCompleted = true'
)
);
}
else {
return [];
}
}.property('content').cacheable(),
# For some reason, this binding works ...
completedLengthBinding: SC.Binding.oneWay('.*completed.length'),
areAllCompleted: function (k, v) {
console.log('get');
if (v !== undefined) {
this.setEach('isCompleted', v);
}
return this.getPath('completed.length') === this.get('length');
# If I reference the binding that references completed, this now works ...
}.property('length','completedLength')
});
为什么这种微妙的差异会突然使它起作用?
谢谢。