Marionette JST模板渲染
Marionette JST template rendering
我正在尝试让 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 传递模板(Marionette 渲染覆盖已删除),即:
class myCompositeView extends Marionette.CompositeView
template: "#mytemplate"
也抛出一个no template error
。我设法传递模板的唯一方法是通过 Underscore 模板构造函数 - _.template()
,这至少表明将集合传递给视图没有问题。
你可以这样做:
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]
我正在尝试让 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 传递模板(Marionette 渲染覆盖已删除),即:
class myCompositeView extends Marionette.CompositeView
template: "#mytemplate"
也抛出一个no template error
。我设法传递模板的唯一方法是通过 Underscore 模板构造函数 - _.template()
,这至少表明将集合传递给视图没有问题。
你可以这样做:
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]