【发布时间】:2013-03-27 00:07:20
【问题描述】:
我正在使用带有 Marionette.CompositeView 的 Handlebars 模板。模板定义为:
template : function (serializedData) {
var templFn = Handlebars.compile(myTemplateDef);
return this.templFn(serializedData);
}
在带有 Handlebars 的传统 Backbone 中,建议不要在每次渲染视图时编译模板,而是将编译后的模板存储为 View 属性,这样它只会编译一次,从而节省资源:
templFn : Handlebars.compile(myTemplateDef),
render : function () {
var serializedData = this.model.toJSON();
...
this.$el.append(this.templFn(serializedData);
}
但在 Marionette 的情况下,template() 的上下文是 window,我无法控制调用 template() 的方式/时间。
所以问题是:鉴于我们不想创建一个全局变量window.templFn,在木偶的情况下,有没有办法将模板编译与其使用分开?
【问题讨论】:
标签: templates backbone.js marionette