在我看来,我正在使用 .ejs 模板。但由于某种原因, View 不会加载给定的模板。它返回未定义。代码如下:
sandplate2.applicationView = Backbone.View.extend({
el: 'div',
template: _.template($("appl.ejs").html()),
initialize: function(){
console.log("Application view initialize");
_.bindAll(this, "render");
this.render();
},
render: function(){
console.log("Application view rendering");
this.$el.html(this.template());
return this;
}
});
我是否需要配置其他内容才能加载模板?
我使用 Yeoman 构建了我的应用程序。我使用了 init 和backbone 生成器。
仅供引用 - 我尝试加载的模板是使用脚本元素加载到 index.html 中的。
最佳答案
如果您使用 Yeoman 构建它,请查看 app.js 看看您是否使用 Backbone.LayoutManager 。您可能必须更改其中的配置才能使 EJS 正常工作。默认情况下,我认为它应该期待 Underscore模板。
我正在使用Handlebars我将 app.js 更新为如下所示:
Backbone.LayoutManager.configure({
manage: true,
paths: {
layout: "templates/layouts/",
template: "templates/"
},
fetch: function(path) {
path = path + ".html";
if (!JST[path]) {
$.ajax({ url: app.root + path, async: false }).then(function(contents) {
JST[path] = Handlebars.compile(contents);
});
}
return JST[path];
}
});
我还将 Handlebars 添加到了模块的 Define() 调用中,并传入“Handlebars”作为引用。您可能需要对 EJS 执行类似的操作。
关于backbone.js - 模板未加载到 Backbone.js 应用程序中 - 使用 Yeoman 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12557699/