简体   繁体   English

Backbone.Marionette和渲染嵌套结构

[英]Backbone.Marionette and rendering nested structures

I'm just getting started with Backbone.Marionette and I'm having trouble rendering a structure with multiple nested items. 我刚开始使用Backbone.Marionette,在渲染具有多个嵌套项目的结构时遇到了麻烦。 I have a TV Guide that has many channels and each channel has many programs. 我的电视指南有很多频道,每个频道都有很多节目。 The json structure is: 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"
      }
    ]
  }
]

Models are setup like this (using coffeescript): 像这样设置模型(使用coffeescript):

class App.Program extends Backbone.Model

class App.Channel extends Backbone.Model
  initialize: ->
    @programs = new App.Programs(@attributes.programs)  

And Collections: 和收藏:

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)

What would be the idiomatic way to render the following structure using Backbone.Marionette views (I left out my view implementation cuz it sucks): 使用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>

When rendered to the DOM, channels will have different event handlers than programs so I need to render them distinctly. 呈现给DOM时,通道将具有与程序不同的事件处理程序,因此我需要分别呈现它们。

Any help would be most appreciated! 非常感激任何的帮助!

Okay, we came up with a solution. 好的,我们提出了一个解决方案。 Here is the code: 这是代码:

%script{type: 'text/html', id: 'channel-template'}
  %td.channel <%= number %>: <%= name %>

%script{type: 'text/html', id: 'program-template'}
  <%= name %>

class App.Program extends Backbone.Model

class App.Programs extends Backbone.Collection
  model: App.Program

class App.ProgramView extends Backbone.Marionette.ItemView
  className: 'program'
  template: "#program-template"
  tagName: 'td'

class App.Channel extends Backbone.Model
  initialize: ->
    programs = @get("programs")
    if programs
      @programs = new App.Programs(programs)

class App.Channels extends Backbone.Collection
  model: App.Channel
  url: -> "http://localhost:3000/guide"

class App.ChannelView extends Backbone.Marionette.CompositeView
  className: 'guide-row'
  itemView: App.ProgramView
  tagName: 'tr'
  template: "#channel-template"

  initialize: ->
    @collection = @model.programs

class App.GuideView extends Backbone.Marionette.CollectionView
  id: '#guide'
  tagName: 'table'
  itemView: App.ChannelView

One of the problems we ran into was the load order of the models, collections and views. 我们遇到的问题之一是模型,集合和视图的加载顺序。 It is very important for the Views. 这对于意见非常重要。 We discovered that if an itemView isn't defined / loaded in a Collection / Composite View, the default is to use the parents template to render the itemView. 我们发现,如果未在Collection / Composite View中定义/加载itemView,则默认值为使用父级模板来呈现itemView。

您可以使用Marionette的CompositeView为模型(整个表格)呈现特定的模板,为任何行呈现特定的View

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM