我刚刚开始使用 Backbone.Marionette,并且在渲染具有多个嵌套项的结构时遇到了麻烦。我有一个有很多频道的电视指南,每个频道都有很多节目。json结构为:
[
{
"name":"HBO",
"number":"541",
"programs":[
{
"name":"Game of Thrones"
},
{
"name":"Gojir returns"
}
]
},
{
"name":"Showtime",
"number":"666",
"programs":[
{
"name":"Alex Cook Saves Space"
},
{
"name":"A Clockwork Orange"
}
]
}
]
模型是这样设置的(使用coffeescript):
class App.Program extends Backbone.Model
class App.Channel extends Backbone.Model
initialize: ->
@programs = new App.Programs(@attributes.programs)
和收藏:
class App.Programs extends Backbone.Collection
model: App.Program
class App.Channels extends Backbone.Collection
model: App.Channel
class App.Guide extends Backbone.Collection
model: App.Channel
url: -> 'http://localhost:3000/guide'
initialize: ->
@on('reset', @setChannels)
setChannels: ->
@channels = new App.Channels(@models)
使用 Backbone.Marionette 视图呈现以下结构的惯用方式是什么(我省略了视图实现,因为它很烂):
<table id="guide">
<thead>
<tr>
<th>Channel</th>
<th>Program 1</th>
<th>Progarm 2</th>
</tr>
</thead>
<tbody>
<tr class="guide-row">
<td class="channel">541:HBO</td>
<td class="program">Game of Thrones</td>
<td class="program">Gojira Returns</td>
</tr>
<tr class="guide-row">
<td class="channel">666:Showtime</td>
<td class="program">Alex Cook Saves Space</td>
<td class="program">A Clockwork Orange</td>
</tr>
</tbody>
</table>
当渲染到 DOM 时,通道将具有与程序不同的事件处理程序,因此我需要清楚地渲染它们。
非常感激任何的帮助!