gpt4 book ai didi

javascript - Handlebars 和嵌套数据

转载 作者:行者123 更新时间:2023-11-28 08:31:33 26 4
gpt4 key购买 nike

我需要帮助,请考虑以下 json 数据:

var datas = {
report_city: 'mycityname1',
sites: [
{ site_name: "site1.com", visits: 100},
{ site_name: "site2.com", visits: 100},
]
};

Handlebars 助手:

Ember.Handlebars.helper('average-visits', function(p_visits, p_sites) {

var allVisits = 0;
p_sites.forEach(function(item, index) {
allVisits = allVisits + parseFloat(item['visits']);
});


var averageVisits = ((p_visits * 100) / allVisits);
return averageVisits;
});

HTML

{{#each sites}}
{{ site_name }} ( {{average-visits visits ../sites }}% )
{{/each}}

为什么不工作? p_site 总是返回未定义

最佳答案

Ember.Handlebars.helper('average-visits', function(p_visits, p_sites) {
var allVisits = 0;
p_sites.forEach(function(item, index) {
allVisits = allVisits + item.visits;
});
console.log('>>> ' + p_visits);


var averageVisits = ((p_visits * 100) / allVisits);
return averageVisits;
});

参见this jsBin 。另一种方法是在 Controller 上定义一个计算属性averageVisits

关于javascript - Handlebars 和嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801603/

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