【发布时间】:2011-08-16 20:29:08
【问题描述】:
Handlebars 无法读取我将其作为上下文发送的 JSON 对象。
这是调用 Mustache 模板并为其提供上下文的函数:
render: function() {
var source = $("#round").html();
var template = Handlebars.compile(source);
var context = JSON.stringify(this.model);
console.log(context);
var html = template(context);
$(this.el).html(html);
return this;
},
这是我传递给它的 JSON 对象:
{"result":0,"friend1":{"firstName":"Ape","lastName":"Head","fbID":329018,"kScore":99,"profilePic":""},"friend2":{"firstName":"Ape","lastName":"Hands","fbID":32,"kScore":70,"profilePic":""}}
这是 Handlebars 模板:
<script id="round" type="text/x-handlebars-template">
{{#with friend1}}
<h2>{{firstName}} {{lastName}}</h2>
{{/with}}
</script>
我收到以下错误:
Uncaught TypeError: Cannot read property 'firstName' of undefined
【问题讨论】:
标签: javascript json backbone.js handlebars.js