我在主干示例中使用 underscore.js 模板库。我的模板如下所示:
<script id="results-template" type="text/template">
<h2><%= title %></h2>
</script>
JSON 对象如下所示:
{"src":"placeholder.jpg","title":"an image placeholder","coordinates":[0,0],"tags":["untagged"],"location":"home"}
我试图通过我的模板解析这个对象,但我通过控制台得到的错误是:
Uncaught ReferenceError: title is not defined
我究竟做错了什么?现场 fiddle 在这里:http://jsfiddle.net/amit_e/muLjV/46/
最佳答案
你的问题是这样的:
JSON.stringify(myPhoto)
这需要
myPhoto.toJSON()
原因:您的
JSON.stringify()
将整个 myPhoto 模型作为 json 字符串 .现在,Backbone 有这个功能可以将 json 输出为 json 对象 ,所以你可以使用 model.toJSON()
更新 jsfiddle:http://jsfiddle.net/saelfaer/muLjV/50/
关于templates - 如何使用 underscore.js 输出 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8724331/