我有这个测试应用程序,每次单击应用程序视图时都应该打印“过滤”和“更改”,因为调用了计算属性。但是,只有在使用空数组更新属性时才会触发属性绑定:
window.App = Ember.Application.create({
Settings: Ember.Object.create({
filter: []
}),
ApplicationView: Ember.View.extend({
click: function(event) {
filter = App.get('Settings.filter');
console.dir(filter);
if (App.get('Room.filtered')) {
filter = filter.filter(function(item) {
return item != App.get('Room.name');
});
} else {
filter.push(App.get('Room.name'));
}
App.set('Settings.filter', filter);
}
})
});
Room = Ember.Object.extend({
name: "test",
_filterer: function() {
console.dir("changed");
}.observes('filtered'),
filtered: function() {
console.dir("filtered");
filter = App.get('Settings.filter');
for (var i = 0; i < filter.length; i++) {
if (filter[i] == this.get('name')) return true;
}
return false;
}.property('App.Settings.filter', 'name').volatile()
});
App.Room = Room.create();
setTimeout(function() {App.set('Settings.filter', ['test']); }, 3000);
这是 jsbin:http: //jsbin.com/edolol/2/edit
为什么只有在将 Setting 设置为空数组时才触发属性绑定?为什么在超时手动执行时会触发?
更新
这是一个有效的 jsbin:http: //jsbin.com/edolol/11/edit