木偶JST模板渲染

时间:2015-09-01 02:36:50

标签: templates backbone.js marionette

我正在尝试让Marionette渲染我的JST模板,在Rails环境中工作。根据教程和Marionette官方文档,我必须覆盖Marionette渲染方法:

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(),这至少表明将集合传递给视图没有问题。

1 个答案:

答案 0 :(得分: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]

SendGrid's document