我正在尝试遍历集合并为集合中的每个模型呈现模板。我究竟做错了什么?
顶层视图:
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"