我正在尝试侦听数组中每个元素的特定属性并从中获取结果。但是,更新似乎没有正确发生。
var emptyEmberObjectClass = Ember.Object.extend({});
var container = Ember.Object.extend({
data: Ember.A([
emptyEmberObjectClass.create({yo:1}),
emptyEmberObjectClass.create({yo:2}),
emptyEmberObjectClass.create({yo:3})
]),
computedData: Ember.computed('data.@each.yo', function(){
var sum = 0;
this.get('data').forEach(function(data){
sum = sum + data.yo;
});
return sum;
}),
test: Ember.observer('computedData', function(){
Ember.$('#a').html('woohoO!');
})
}).create();
var existingItem = container.get('data');
existingItem.objectAt(0).set('yo', 50);
http://jsfiddle.net/stb0nr2y/1/
正如您所看到的,文字字段仍然显示“开始”#39;并没有更新到' woohoO!'。
非常感谢任何帮助。
答案 0 :(得分:1)
JavaScript的:
var emptyEmberObjectClass = Ember.Object.extend({});
var container = Ember.Object.extend({
data: Ember.A([
emptyEmberObjectClass.create({yo:1}),
emptyEmberObjectClass.create({yo:2}),
emptyEmberObjectClass.create({yo:3})
]),
computedData: Ember.computed('data.@each.yo', function(){
var sum = 0;
this.get('data').forEach(function(data){
sum += data.get('yo');
});
return sum;
}),
testObs: Ember.on('init', Ember.observer('computedData', function() {
Ember.$('#a').html('woohoO! cd: `' + this.get('computedData') + '`');
}))
}).create();
var existingItem = container.get('data');
var target = existingItem.objectAt(0);
target.set('yo', 50);
target.set('yo', 100);
使用以下方法正确更新DOM:
哇噢! cd:
55
然后:
哇噢! cd:
105
请注意我使用的是Ember v1.13.10:
<script src="http://builds.emberjs.com/tags/v1.13.10/ember.min.js"></script>