gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:20:25 24 4
gpt4 key购买 nike

如何在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com