[英]Handlebars.js Cannot read property 'hash' of undefined
There's this code: 有以下代码:
<div id="banner-tree" class="hidden"></div>
<script id="banner-template" type="x-handlebars-template">
{{#each this}}
{{#if this.text and this.outcomes}}
<div class="banner-wrap" style="background-image: url({{imageSrc}});">
<div class="banner-desc">{{text}}</div>
<div class="banner-markets">Odds: {{outcomes.outcomeOdds}}</div>
</div>
{{else if this.outcomes}}
<div class="banner-wrap" style="background-image: url({{imageSrc}});">
<div class="banner-markets">Odds: {{outcomes.outcomeOdds}}</div>
</div>
{{else if this.text}}
<div class="banner-wrap" style="background-image: url({{imageSrc}});">
<div class="banner-desc">{{text}}</div>
</div>
{{else}}
<div class="banner-wrap" style="background-image: url({{imageSrc}});"></div>
{{/if}}
{{/each}}
</script>
And this script: 这个脚本:
var templateBanner = $("#banner-template").html();
var templateCompiled = Handlebars.compile(templateBanner);
var compiledHtml = templateCompiled(response.data);
var bannerTree = $('#banner-tree');
bannerTree.html(compiledHtml);
bannerTree.removeClass('hidden');
Handlebars.registerHelper('if', function(conditional, options) {
if(conditional) {
return options.fn(this);
} else {
return options.reverse(this);
}
});
Handlebars.registerHelper('each', function(context, options) {
var ret = "";
for(var i=0, j=context.length; i<j; i++) {
ret = ret + options.fn(context[i]);
}
return ret;
});
Yesterday it was all right. 昨天没事。 It worked as it should.
它按预期工作。 This morning I started getting
Cannot read property 'hash' of undefined
. 今天早上,我开始
Cannot read property 'hash' of undefined
。 After some inquiring, I figured the fault is at {{#each this}}
element. 经过一番询问,我发现问题出在
{{#each this}}
元素上。 Or I guess so at least. 或者我猜至少如此。 When I change context, say, for
data
it doesn't produce any results (loaded JSON arrays are NOT pushed into div), but it doesn't give me error either. 例如,当我更改上下文时,对于
data
它不会产生任何结果(加载的JSON数组不会被推入div),但也不会给我错误。 When I remove whole that ifology and leave just 当我删除整个假设时,只留下
{{#each this}}
<div class="banner-wrap" style="background-image: url({{imageSrc}});"></div>
{{/each}}
Then it works partially. 然后它会部分工作。 But unfortunately, I need those ifs.
但不幸的是,我需要那些前提条件。 So, it's eather if or each.
因此,无论是否都可以。 I'm trying to solve this for last two hours and I came up empty :/
我试图解决最后两个小时的问题,但我还是空了:/
{{#if this.text and this.outcomes}}
was at fault. {{#if this.text and this.outcomes}}
。 After redefining the whole statements just to include partial if
s and else
it started to work just fine. 在重新定义整个语句后,只包含了部分
if
, else
它开始正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.