2014-01-23 85 views
0

我最近开始在Backbone Library中工作。目前我正在研究的骨干视图有子视图,所以我认为我可以覆盖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

我不认为顺序的问题,所有的删除所做的就是调用,

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); 
}