我遇到了使用bindAll的問題。我得到的錯誤是func is undefined
。任何想法我做錯了什麼?Backbone和bindAll:「func is undefined」
我都試過
bindAll
(失敗並出現上述錯誤)和- 個人
bind
秒(不工作)
window.test = Backbone.View.extend({
collection: null
initialize: ->
console.log('initialize()')
console.log(this)
# _.bindAll(this, ["render", "foo"])
_.bind(this.render, this) # these don't throw errors, but binding won't work
_.bind(this.foo, this)
@collection = new Backbone.Collection()
@collection.bind "add", @render
@collection.bind "add", @foo
@render()
foo: ->
# won't be bound to the view when called from the collection
console.log("foo()")
console.log(this)
console.log(this.collection) # undefined (since this is the collection, not the view)
render: ->
console.log("render()")
console.log(this)
return this
})
testInstance = new window.test();
# using _.bind instead of bindAll we get past this point, however this won't be the view
testInstance.collection.add({})
還沒有設法讓胖箭頭在Backbone模型'方法'上工作。 你說得對,我的bindAll語法錯了。謝謝! – sandstrom
胖箭頭也解決了我的問題,謝謝! – pws5068