【问题标题】:Precompiled Handlebars template with RequireJS使用 RequireJS 预编译的 Handlebars 模板
【发布时间】:2013-08-19 05:14:57
【问题描述】:

我有一个手动预编译并保存为的车把模板 - testTemplate.handlebars。

现在,在我的 requireJS + Backbone 代码中,我有以下函数 -

define(['text!../templates/testTemplate.handlebars'
       ],function(testTemplate){

           var myView = Backbone.View.extend(

              initialize: function(options){

                  this.template = Handlebars.template(testTemplate);

              },

              render: function(data){

                  $(this.el).html(this.template(data));
              }

           );
});

所以 testTemplate.handlebars 以字符串形式返回 Javascript 代码,当传递给 Handlebars.template 时返回 JS 函数。当我尝试在控制台上打印我在 this.template 变量中得到的值时,它会显示 -

function (n,r){return r=r||{},e.call(t,Handlebars,n,r.helpers,r.partials,r.data)}

但是,当 render 函数的 $(this.el).html(this.template(data)); 行执行时,它会给出一条错误消息 - Uncaught Typeerror : object has no method call。 (尽管我可以看到一个 e.call 函数)

我在这里做错了吗?

另外,当我尝试编译模板运行时,渲染函数可以工作。 在运行时编译 Handlebars.compile(testTemplate) 时返回以下函数 -

function (e,t){return n||(n=r()),n.call(this,e,t)}

【问题讨论】:

    标签: javascript backbone.js requirejs handlebars.js precompiled-templates


    【解决方案1】:

    如果您已预编译它,我不确定您是否需要进行 .template 调用。您获得的功能应该可以作为模板本身执行。所以这个:

    $(this.el).html(this.template(data));
    

    变成这样:

    $(this.el).html(testTemplate(data));
    

    【讨论】:

      猜你喜欢
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2016-07-20
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多