javascript - 如何访问 Handlebars 模板中的功能?

标签 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;    
    }
}

最佳答案

将函数创建为属性。

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/

相关文章:

javascript - 在页面刷新时保留 cookie 存储中的范围值

javascript - 如何一起使用 getElementById 和 getElementsByTagName 获取元素的值

jquery - Node JS 排序数组通过 Handlebars 模板客户端传递

css - Handlebars+BootStrap卡边框

JavaScript onkeydown, 返回 true,false

javascript - javascript、jquery 或 django 中的 HTTP 基本身份验证?

javascript - 滚动时修复侧边栏,直到到达页脚(使用 Bootstrap 类)

node.js - 查找 View 失败

javascript - 检查 Emberjs 中的组件分配了哪些属性

ember.js - 如何使用 Ember js 和 Handlebars js 将文本换行?