2

entries/index加载后,它应该触发并将@collection.each(@appendEntry)集合中的所有条目附加到#entries,但没有任何反应。如果我提交一个新条目,一切正常。

entries_index.js.coffee

class Raffler.Views.EntriesIndex extends Backbone.View

  template: JST['entries/index']

  events:
    'submit #new_entry':  'createEntry'

  initialize: ->
    @collection.on('add', @render, this)

  render: ->
    $(@el).html(@template(collection: @collection))
    @collection.each(@appendEntry)
    this

  appendEntry: (entry) ->
    view = new Raffler.Views.Entry()
    $('#entries').append(view.render().el)

  createEntry: (event) ->
    event.preventDefault()
    @collection.create name: $('#new_entry_name').val()

这里发生了什么?

4

1 回答 1

0

尝试renderinitialize.

@collection.each(@appendEntry)位于您的渲染函数中,在initialize.

添加项目后您可以看到它的原因是每次通过绑定将项目添加到您的集合时都会调用渲染:@collection.on('add', @render, this)

于 2013-05-20T04:43:03.930 回答