Backbone.js - Remove all sub views

后端 未结 4 1438
一个人的身影
一个人的身影 2021-01-30 23:51

I have a top level PageView that will re-render itself whenever the route changes. I have many nested sub-views embedded into this PageView. If I was to re-render PageView, do

4条回答
  •  我在风中等你
    2021-01-31 00:13

    A bit like Zengineer wrote, i like to patch Backbone.View.remove globally like the following, so that any child views attached on this are removed

    var originalRemove = Backbone.View.prototype.remove;
    
    Backbone.View.prototype.remove = function ()
    {
    
      for (var view in this){
        if (this[view] instanceof Backbone.View && this[view] != this) {
            this[view].remove();
        } 
      }
    
    
      originalRemove.apply(this, arguments);
    
    }
    

提交回复
热议问题