【发布时间】:2011-09-30 13:59:06
【问题描述】:
我在使用 bindAll 时遇到了问题。我得到的错误是func is undefined。对我做错了什么有什么想法吗?
我都试过了
-
bindAll(因上述错误而失败)和 - 个人
binds(不工作)
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({})
【问题讨论】:
标签: javascript binding backbone.js coffeescript underscore.js