删除主干视图和解除绑定事件

时间:2014-01-23 13:09:13

标签: javascript backbone.js

我最近开始在Backbone Library工作。目前我正在研究的Backbone View有子视图,所以我认为我可以覆盖remove()方法并在那里进行清理工作。

这是代码,

var myView = Backbone.View.extend({
   ...

   remove: function(){
      this.childView1.remove();
      this.childView2.remove();

      Backbone.View.prototype.unbind.call(this);
      Backbone.View.prototype.remove.call(this);
   }
});

在SO中查看一些示例时,调用unbind()remove()的顺序相反意味着首先调用remove()然后调用unbind()。我上面的命令是正确还是错误?

1 个答案:

答案 0 :(得分:1)

我认为订单不重要,所有删除都是电话,

remove: function() {
  this.$el.remove();
  this.stopListening();
  return this;
}

从DOM中删除元素并删除所有侦听器

.off().unbind()只是.off()的掩码)对象只有removes all bound callbacks for all events。订单无关紧要,因为结果将是相同的。

应该注意的是,只要您使用this.listenTo()注册所有的事件监听器(您真的应该这样做),您甚至不需要致电.off()

remove: function(){
   this.childView1.remove();
   this.childView2.remove();
   return Backbone.View.prototype.remove.call(this);
}
相关问题