我正在使用 Backbone 和 Underscore 创建一个小型测试站点。
我正在按照此处和此处的建议将我的所有 html 模板文件编译成一个 JST javascript 文件。
然而,如何将其与模板文件一起使用并不是很明显。我试过这个:
App.HeaderView = Backbone.View.extend({
el: '#headerContent',
template: JST['header.html'](),
//template: window["JST"]["header.html"],
//template: _.template("<h1>Some text</h1>"),
initialize: function () {
this.render();
},
render: function() {
//var html = this.template();
//// Append the result to the view's element.
//$(this.el).append(html);
this.$el.html(this.template());
return this; // enable chained calls
}
});
我得到的错误是 JST.header.html is not a function。
(顺便说一句,最后注释掉的部分有效,template: _.template("<h1>Some text</h1>")所以我知道问题不在于其他问题)。
可能是因为我正在使用 browserify(所以尝试了“要求”该文件),但我尝试了几种不同的“包含”模板文件的方法,包括直接添加它:
<script src="templates/_templates.js"></script>
<script src="js/functions.js"></script>
</body>
</html>
有什么想法需要做些什么才能让它发挥作用?