繁体   English   中英

带有RequireJS的预编译Handlebars模板

[英]Precompiled Handlebars template with RequireJS

我有一个手动模板,我手动预编译并保存为 - 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)}

但是,当行 - $(this.el).html(this.template(data)); 渲染函数执行时,它会给出一条错误消息 - Uncaught Typeerror:object没有方法调用。 (即使我能看到一个e.call函数)

我在这里做错了吗?

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

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

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

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

成为这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM