如何在Handlebars模板中访问功能?

时间:2014-11-26 05:46:19

标签: javascript handlebars.js

如何在Handlebars模板中获取getSum()的值?

当我致电{{App.Address.getsum}}时,它会返回字符串。它包括所有功能部分。 我怎样才能得到正确的值?

我试图获得getsum的价值。

App.Address = {    
    details: [
        {
            "time": "11.56",
            "size": 10540,
            "price": "789.5",
            "direction": "Down",
            "side": "Sell",
            "change": "9.5",
            "check": "true"
        },
        {
            "time": "14.56",
            "size": 145500,
            "price": "555.5",
            "direction": "Up",
            "side": "Buy",
            "change": "5.5"
        }, {
            "time": "15.56",
            "size": 15550,
            "price": "456",
            "direction": "Down",
            "side": "Sell",
            "change": "9.5",
            "check": "true"
        }, {
            "time": "11.45",
            "size": 14550,
            "price": "23.5",
            "direction": "Down",
            "side": "Buy",
            "change": "5.5"
        }, {
            "time": "11.22",
            "size": 54500,
            "price": "2.5",
            "direction": "Down",
            "side": "Sell",
            "change": "6.8",
            "check": "true"
        }, {
            "time": "20.56",
            "size": 110550,
            "price": "11.8",
            "direction": "Down",
            "side": "Buy",
            "change": "3.5"
        }, {
            "time": "11.13",
            "size": 14343432,
            "price": "88.8",
            "direction": "Down",
            "side": "Buy",
            "change": "2.5"
        },

        {
            "time": "13.56",
            "size": 23423434,
            "price": "855.8",
            "direction": "Up",
            "side": "Sell",
            "change": "1.8",
            "check": "true"
        }, {
            "time": "11.33",
            "size": 233,
            "price": "86.5",
            "direction": "Up",
            "side": "Buy",
            "change": "6.8"
        }, {
            "time": "11.56",
            "size": 153243,
            "price": "28.5",
            "direction": "Up",
            "side": "Buy",
            "change": "5.8"
        }, {
            "time": "11.15",
            "size": 1344,
            "price": "456",
            "direction": "Down",
            "side": "Buy",
            "change": "1.1"
        }
    ],
    getSum: function() {
        var details = this.details;
        var sum = 0;

        for (var i = 0, length = details.length; i < length; i++) {
            sum += parseInt(details[i].size, 10);
        }
        return sum;    
    }
}

1 个答案:

答案 0 :(得分:0)

将该函数创建为属性。

getSum: function() {
        var details = this.details;
        var sum = 0;

        for (var i = 0, length = details.length; i < length; i++) {
            sum += parseInt(details[i].size, 10);
        }
        return sum;
    }.property()

然后你可以通过模板中的{{App.Address.getSum}}来调用它。