如何在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;
    }
}

最佳答案

将函数创建为属性。

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}}进行调用。

关于javascript - 如何访问 Handlebars 模板中的功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27142147/

10-12 15:19