我想绑定到ArrayProxy的属性,以获取作为该Array成员的Object的计算属性。我在这里设置了一个JS小提琴:http://jsfiddle.net/sohara/MZeUu/31/
正如您在下面的代码中看到的,我的计算属性取决于thingsController中的聚合计算属性。但是,如果单击删除按钮,则不会再次调用percentWeight计算机属性,因为我不知道如何将此函数绑定到thingsController中的totalWeight计算属性。我确定有办法做到这一点.....任何想法?
window.App = Ember.Application.create();
App.Thing = Ember.Object.extend({
weight: null,
percentWeight: function() {
return (this.get('weight') / App.thingsController.get('totalWeight')) * 100;
}.property('weight')
// This below doesn't work - can't access this property error
//}.property('weight', 'App.thingsController.totalWeight')
});
App.thingsController = Ember.ArrayProxy.create({
content: [
App.Thing.create({weight: 100}),
App.Thing.create({weight: 200}),
App.Thing.create({weight: 300}),
App.Thing.create({weight: 400})
],
totalWeight: function() {
var totalWeight = 0;
this.get('content').forEach(function(item) {
totalWeight += item.get('weight');
});
return totalWeight;
}.property('@each.weight'),
destroy: function(item) {
this.removeObject(item);
}
});
App.ThingView = Ember.View.extend({
deleteButton: Ember.Button.extend({
click: function(event) {
var item = this.get('content');
App.thingsController.destroy(item);
}
})
});
答案 0 :(得分:1)
您遗失的一个绑定=> totalWeightBinding和一些代码重组,以便App.Thing知道App.thingsController。
<script type="text/javascript">
App.thingsController = Ember.ArrayProxy.create({
totalWeight: function() {
var totalWeight = 0;
this.get('content').forEach(function(item) {
totalWeight += item.get('weight');
});
return totalWeight;
}.property('@each.weight'),
destroy: function(item) {
this.removeObject(item);
}
});
App.Thing = Ember.Object.extend({
weight: null,
totalWeightBinding: 'App.thingsController.totalWeight',
percentWeight: function() {
return (this.get('weight') / this.get('totalWeight')) * 100;
}.property('weight')
});
var things = [
App.Thing.create({weight: 100}),
App.Thing.create({weight: 200}),
App.Thing.create({weight: 300}),
App.Thing.create({weight: 400})
]
App.thingsController.set('content', things);
</script>