使用RequireJS预编译的Handlebars模板
Precompiled Handlebars template with RequireJS
我有一个手把模板,我已经手动预编译并保存为-testTemplate.handlebars.
现在,在我的requireJS+主干代码中,我有以下函数-
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));
行执行时,它会给出一条错误消息,称UncaughtTypeerror:object没有方法调用。(尽管我可以看到e.call函数)
我是不是做错了什么?
此外,当我尝试编译模板运行时,render函数也能工作。当运行时编译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));
相关文章:
- 用于在Backbone中创建列表的Handlebars模板
- HandleBars-模板中的模板
- Handlebars模板未出现
- metalsmith系列'路径'无法从Handlebars模板访问键
- Handlebars模板未填充上下文
- helpers字符串中的Handlebars模板标记
- Handlebars模板:将多个数组中的值打印到一个表中
- 如何在编译后将事件侦听器添加到Handlebars模板中
- 如何在Handlebars模板中使用metalsmith永久链接
- 如何为Handlebars模板设置默认参数
- 在Handlebars模板中执行Javascript
- 使用RequireJS预编译的Handlebars模板
- 对所有页面使用相同的Handlebars模板
- Handlebars模板,使用字符串键访问上下文变量
- Handlebars模板工作不正常
- Handlebars模板管理
- 骨干事件不'无法在Handlebars模板中工作
- 如何在Handlebars模板中初始化jQuery小部件
- Ember Computed属性和Handlebars模板
- Handlebars模板占位符的默认值