如何获取在视图集合的属性上计算的属性

时间:2012-01-25 17:53:36

标签: ember.js

我有一个这样的视图对象:

App.ThingView = Ember.View.extend({
    things: App.thingsController,
    weightBinding: 'content.weight',
    unitConversionFactorBinding: 'App.thingsController.unitConversionFactor',

    percentWeight: function() {
        var weight = this.getPath('content.weight');
        var totalWeight = this.getPath('things.totalWeight');
        return (weight / totalWeight) * 100;
    }.property('content.weight', 'things.totalWeight'),

    weightUnits: function() {
        var weight = this.getPath('content.weight');
        var unitConversionFactor = this.get('unitConversionFactor');
        return weight * unitConversionFactor;
    }.property('content.weigth', 'unitConversionFactor'),

});

此视图用于迭代控制器数组,如下所示:

{{#each App.thingsController tagName="ul"}}
    {{#view App.ThingView contentBinding="this"}}
            {{weight}} - {{percentWeight}}  % - {{weightUnits}}
            {{#view deleteButton}}Delete{{/view}}
    {{/view}}
{{/each}}

我想显示weigthUnits的总数,基本上迭代#each帮助器渲染的视图并添加它们。这可能吗?或者我是否需要恢复将所有这些计算属性逻辑放入模型中(以便从控制器访问聚合计算)?我在这里设置了一个jsfiddle: http://jsfiddle.net/sohara/MuwDd/19/

1 个答案:

答案 0 :(得分:0)

这个怎么样:http://jsfiddle.net/tomwhatmore/fqR8W/1/

现在赶时间,但我觉得js小提琴涵盖了吗?如上所述,将weightUnit逻辑移动到模型中。