骨干网和bindAll:" FUNC是未定义的" [英] Backbone and bindAll: "func is undefined"

查看:97
本文介绍了骨干网和bindAll:" FUNC是未定义的"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了bindAll问题。我得到的错误是 FUNC未定义。对我在做什么任何想法错了吗?

我都试过


  • bindAll (失败并出现上述错误)和

  • 个人绑定 S(不工作)

window.test = Backbone.View.extend({  采集日期null  初始化: - >
    的console.log('初始化())
    的console.log(本)
    #_.bindAll(这一点,[渲染,富])
    _.bind(this.render,这一点)#这些不抛出错误,但绑定将无法工作
    _.bind(this.foo,这一点)
    @collection =新Backbone.Collection()
    @ collection.bind增加,@render
    @ collection.bind增加,@foo
    @render()  富: - >
    #将不会被绑定到视图从集合调用时
    的console.log(富())
    的console.log(本)
    未定义的console.log(this.collection)#(因为这是收藏,而不是视图)  渲染: - >
    的console.log(渲染())
    的console.log(本)
    返回此})
testInstance =新window.test();
使用#_.bind代替bindAll的,我们闯过了这一点,但是这不会是视图
testInstance.collection.add({})


解决方案

您正在使用的CoffeeScript,你不需要下划线的绑定。 CoffeeScript中有它内置的。只要使用胖箭头

  foo的:=>
    #Your foo的实现放在这里
渲染:=>
    #你的渲染实现放在这里

搜索胖箭头在这里: http://jashkenas.github.com/coffee-script/

不过,关于 _。bindAll ,你并不需要提供方法名作为数组。你可以做任何 _。bindAll(本) _。bindAll(这一点,'渲染','富')(方法名是变参,没有一个明确的列表)。看看是否有帮助。

I've got problems using bindAll. The error I get is func is undefined. Any thoughts on what I'm doing wrong?

I've tried both

  • bindAll (fails with the above error) and
  • individual binds (don't work)

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({})

解决方案

You are using CoffeeScript, you don't need underscore's bind. CoffeeScript has it built in. Just use "fat arrows"

foo: =>
    #Your foo implementation goes here
render: =>
    # your render implementation goes here

Search for "fat arrow" here: http://jashkenas.github.com/coffee-script/

However, with regards to _.bindAll, you don't need to provide the method names as an array. You can do either _.bindAll(this) or _.bindAll(this, 'render', 'foo') (method names are var args, not an explicit list). See if that helps.

这篇关于骨干网和bindAll:" FUNC是未定义的"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆