【问题标题】:Backbone - Uncaught TypeError: Cannot call method 'unbind' of undefined骨干 - 未捕获的类型错误:无法调用未定义的方法“取消绑定”
【发布时间】:2013-05-31 06:47:23
【问题描述】:

使用 Backbone 0.9.2 我试图取消绑定视图中的某些元素,但出现以下错误

未捕获的类型错误:无法调用未定义的方法 'unbind'


      console.log('+++ Kill: ', this);
        this.model.unbind( 'change', this.render, this ); // Unbind reference to the model
        this.options.parent.unbind( 'close:all', this.close, this ); // Unbind reference to the parent view     
        delete this.$el; // Delete the jQuery wrapped object variable
        delete this.el; // Delete the variable reference to this node

知道如何解决这个错误吗?

【问题讨论】:

  • 哪一行导致了错误? “this.model”还是“this.options.parent”?在控制台输出这两个,检查是否定义了
  • line this.model.unbind
  • 这意味着,您的视图没有模型,也许它没有被设置或以某种方式被删除。你最好在这个过程之前检查逻辑。
  • 您很可能在回调中使用 this.model 并且忘记将该函数绑定到 this

标签: javascript backbone.js


【解决方案1】:

嗯,很明显this.model 不是你想的那样。也许this 不是您的视图实例?在任何情况下,只需在视图的initialize 中处理模型事件,如下所示:

initialize: function () {
    this.listenTo(this.model, 'change', this.render, this);
}

你会没事的,因为它们会在remove 调用stopListening 时自动解除绑定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2012-03-09
    • 2013-08-31
    • 2011-11-12
    • 2013-09-14
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多