您好,我一直在尝试使用backbonejs和 Handlebars 模板,但似乎我的json错误或数据未正确解析。获取
Uncaught Error: You must pass a string to Handlebars.compile. You passed undefined
代码可以在
找到如有任何建议,我们将不胜感激
最佳答案
您的 fiddle 以各种方式损坏,但很可能您正在这样做:
template: Handlebars.compile($('#tpl-page-list-item').html()),
之前有一个#tpl-page-list-item
可用的。如果您的页面如下所示,就会发生这种情况:
<script src="your_backbone_javascript.js"></script>
<script id="tpl-page-list-item" ...></script>
因为您的 Backbone View 将在 <script id="tpl-page-list-item">
之前被解析被添加到 DOM 中。您可以将 Backbone View 包装在文档就绪处理程序中(使用适当的命名空间来说明函数包装器):
$(function() {
window.PageListItemView = Backbone.View.extend({
template: Handlebars.compile($('#tpl-page-list-item').html()),
//...
});
});
或者在实例化 View 时编译模板:
initialize: function() {
// Or check if it is in the prototype and put the compiled template
// in the prototype if you're using the view multiple times...
this.template = Handlebars.compile($('#tpl-page-list-item').html());
//...
}
关于javascript - Handlebars 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19845027/