Backbone.js 主干视图方法在第一次加载时未触发
加载Backbone.js 主干视图方法在第一次加载时未触发,backbone.js,coffeescript,Backbone.js,Coffeescript,加载entries/index时,它应该触发@collection。每个(@appendEntry)都会将集合中的所有条目追加到#entries,但不会发生任何事情。如果我提交一个新条目,一切正常 条目\u index.js.coffee: class Raffler.Views.EntriesIndex extends Backbone.View template: JST['entries/index'] events: 'submit #new_entry': 'cr
entries/index
时,它应该触发@collection。每个(@appendEntry)
都会将集合中的所有条目追加到#entries
,但不会发生任何事情。如果我提交一个新条目,一切正常
条目\u 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()
这是怎么回事?尝试在
初始化中调用渲染
@collection。每个(@appendEntry)
都在渲染函数中,视图在初始化过程中不会调用该函数
添加项后可以看到它的原因是,每次将项添加到集合时都会通过绑定调用render:@collection.on('add',@render,this)
我不确定咖啡脚本语法,但如何使用传递给appendEntry
的entry
参数?