1

我正在尝试遍历集合并为集合中的每个模型呈现模板。我究竟做错了什么?

顶层视图:

class App.Views.Content extends Backbone.View
  className: 'views-container'

  template:  HandlebarsTemplates['app/templates/content']

  render: ->
    @$el.html(@template())
    @renderEmptyView()
    @renderLanParties()
    @

  renderEmptyView: ->
    v = new App.Views.Empty()
    @$('.content-wrapper').html(v.render().el)

  renderLanParties: ->
    v = new App.Views.LanParties({ collection: new App.Collections.LanParties })
    @$('.lan-list').html(v.render().el)

查看渲染集合:

class App.Views.LanParties extends Backbone.View
  className: 'lan-parties-list'

  template:  HandlebarsTemplates['app/templates/lan_parties']

  initialize: ->
    @listenTo @collection, 'reset', @render()
    @collection.fetch({ reset: true })

  render: ->
    @$el.html(@template())
    @collection.forEach @renderLanParty, @
    @

  renderLanParty: (model) ->
    v = new App.Views.LanParty({ model: model })
    @$('ul').append(v.render().el)

该模型:

class App.Models.LanParty extends Backbone.Model

class App.Collections.LanParties extends Backbone.Collection
  model:  App.Models.LanParty
  url: "/lan_parties"
4

1 回答 1

0

在 App.Views.LanParties 中不要调用 @render,更改

@listenTo @collection, 'reset', @render()

@listenTo @collection, 'reset', @render

您可能希望在 App.Views.Content 中实例化新的 App.Collections.LanParties。

于 2013-08-23T10:28:53.740 回答