我需要帮助,请考虑以下json数据:
var datas = {
report_city: 'mycityname1',
sites: [
{ site_name: "site1.com", visits: 100},
{ site_name: "site2.com", visits: 100},
]
};
把手助手:
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始终返回undefined
答案 0 :(得分:0)
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。
另一种方法是在控制器上定义计算属性averageVisits
。