我试图遍历一个集合并为该集合中的每个模型渲染模板。我究竟做错了什么?

顶层视图:

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"

最佳答案

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

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




@listenTo @collection, 'reset', @render


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

关于javascript - 未捕获的TypeError:对象#<LanParties>没有方法'call',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18398408/

10-11 15:56