backbone.js - 模板未加载到 Backbone.js 应用程序中 - 使用 Yeoman 构建

标签 backbone.js yeoman

在我看来,我正在使用 .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/

相关文章:

node.js - 全栈 Angular 和meanjs之间的区别

coffeescript - Yeoman 生成器 CoffeeScript 入口点

javascript - 中止访问 Backbone 模型中的成功回调

jquery - backbone.js:渲染后将焦点设置为输入

javascript - javascript 中特定调用的所有 ajax 回调的一个入口点

javascript - 我的第一个主干模型/ View 。我在正确的轨道上吗?

node.js - jhipster 不是内部或外部命令,也不是可运行的程序或批处理文件

css - 如何将 Materialize CSS 添加到 AngularJS 元素?

javascript - Backbone : using partial fetch versus 1 collection through the whole lifespan of the application

node.js - 运行 "npm ERR!"时有很多 `yo angular`