如何渲染骨干灰尘模板 [英] How to render dust template with backbone
问题描述
我想显示一个骨干视图中的灰尘模板(编译)。
I'm trying to display a dust template (compiled) within a backbone view.
这是我在视图渲染功能:
here is my render function in the view :
render: ->
dust.render("customer-item", @model.toJSON(), (err, output) ->
throw err if err
$(@el).html output
)
@
在我带领的观点我看到2的div我的容器中添加(对应于应加载了2款),但它们都是空的。个别模板不redered ...
当我调试输出变量我看到模板所以理论上它应该被正确加载中...
When i lead the view i see 2 divs added inside my container (corresponding to the 2 models that should be loaded) but they are both empty. The individual template is not redered... When I debug the "output" variable i see the template so in theory it should be loaded properly...
当我做一个简单的测试的我可以看到输出
When I do a simple test I can see an output for :
render: ->
$(@el).html "<span>TEST</span>"
@
但对于该方案的我有没有输出
render: ->
dust.render("customer-item", @model.toJSON(), (err, output) ->
$(@el).html "<span>TEST</span>"
)
@
您的帮助非常感谢。
Many thanks for your help.
推荐答案
我没用过灰尘那么多,但对于我记得你应该先编译模板一样,(在普通的JavaScript):
I didn't used Dust that much but for what I remember you should compile your template first like that (in plain Javascript):
var source = $("#some-template").html();
var compiled = dust.compile(source,"table");
dust.loadSource(compiled);
dust.render("table",this.model.toJSON(),function(err,out){
$(el).html(out);
});
编辑:
我想我找到了问题所在:当你调用 $(this.el)的.html
(下)你dust.render函数中,这个
是断章取义。所以,你必须渲染功能更改为类似的东西:
I think I found where the problem lies: when you call $(this.el).html(out)
inside your dust.render function, this
is out of context. So you have to change your render function to something like that:
var self = this;
dust.render("table",this.model.toJSON(),function(err,out){
$(self.el).html(out);
});
这篇关于如何渲染骨干灰尘模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!