【问题标题】:Marionette JST template renderingMarionette JST 模板渲染
【发布时间】:2015-09-01 02:36:49
【问题描述】:

我正在尝试让 Marionette 在 Rails 环境中渲染我的 JST 模板。根据教程和 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(),这至少表明将集合传递给视图没有问题。

【问题讨论】:

    标签: templates backbone.js marionette


    【解决方案1】:

    你可以这样:

    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

    【讨论】:

    • 哦,非常感谢。我已经解决了它,但是您的方法更具可配置性!如果您不介意,在相关说明中,您如何在 JST 模板中使用 Rails 助手?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 2015-07-23
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多