2015-09-01 97 views
0

我试图让木偶渲染我的JST模板,在Rails环境中工作。按照教程和木偶官方文档我要重写木偶渲染方法:Marionette JST模板渲染

Backbone.Marionette.Renderer.render = (template, data) -> 
path = JST["path/to/template/" + template] 
unless path 
    throw "error" 
path(data) 

而且从视图中调用模板时:

class myChildView extends Marionette.ItemView 
     template: "specific-template-location/template" 

    class myCompositeView extends Marionette.CompositeView 
     template: "specific-template-location/template" 
     childView: myChildView 

我在渲染时得到一个Uncaught error。奇怪的是,但当我用itemView而不是childView模板正常渲染。我正在使用的教程已被证明已过时,但我在官方文档中找不到childView \ itemView与模板声明之间的任何相关性。任何提示将非常感谢。

其他信息:我也不能直接从DOM通过模板(木偶渲染倍率删除),即:

class myCompositeView extends Marionette.CompositeView 
     template: "#mytemplate" 

此外抛出no template error。我设法传递模板的唯一方法是通过一个Underscore模板构造函数 - _.template(),它至少表示将集合传递给视图没有问题。

回答

0

你可以做这样的:

do (Marionette) -> 
    _.extend Marionette.Renderer, 
    lookups: ['path/to/template/apps', 'path/to/template/components'] 

    render: (template, data) -> 
    return unless template 
    path = @getTemplate(template) 
    throw "Template #{ template } not found!" unless path 
    path(data) 

    getTemplate: (template) -> 
    for lookup in @lookups 
     path = "#{ lookup }/#{ template }" 
     return JST[path] if JST[path] 

I have one example here, using Rails too

+0

哦,谢谢了一堆。我已经解决了它,但您的方法更具可配置性!如果您不介意在相关说明中介绍如何在JST模板中使用Rails帮助程序? –