Ticket #90: 90.patch

File 90.patch, 7.5 kB (added by tschaub, 3 years ago)

proper destroy and listener handling

  • tests/lib/GeoExt/widgets/tree/LayerContainer.html

    old new  
    5454            t.ok(node.firstChild.layer === layer, "child layer is correct"); 
    5555 
    5656            node.destroy(); 
    57             //map.destroy(); 
     57            map.destroy(); 
    5858             
    5959        } 
    6060         
     
    117117            t.eq(root.childNodes[3].layer.name, "c", "[c, a, b, d] first layer drawn at bottom of root"); 
    118118             
    119119            root.destroy(); 
    120             //map.destroy(); 
     120            map.destroy(); 
    121121             
    122122        } 
    123123         
  • lib/GeoExt/widgets/tree/LayerContainer.js

    old new  
    100100        } 
    101101    }, 
    102102 
    103     /** private: method[onDestroy] 
    104      */ 
    105     onDestroy: function() { 
    106         if(this.layerStore) { 
    107             this.layerStore.un("add", this.onStoreAdd, this); 
    108             this.layerStore.un("remove", this.onStoreRemove, this); 
    109         } 
    110         GeoExt.tree.LayerContainer.superclass.onDestroy.apply(this, arguments); 
    111     }, 
    112      
    113103    /** private: method[recordIndexToNodeIndex] 
    114104     *  :param index: ``Number`` The record index in the layer store. 
    115105     *  :return: ``Number`` The appropriate child node index for the record. 
     
    220210            this.layerStore.insert(newRecordIndex, [record]); 
    221211            delete this._reordering; 
    222212        } 
     213    }, 
     214 
     215    /** private: method[destroy] 
     216     */ 
     217    destroy: function() { 
     218        if(this.layerStore) { 
     219            this.layerStore.un("add", this.onStoreAdd, this); 
     220            this.layerStore.un("remove", this.onStoreRemove, this); 
     221        } 
     222        GeoExt.tree.LayerContainer.superclass.destroy.apply(this, arguments); 
    223223    } 
    224      
    225224}); 
    226225 
    227226/** 
  • lib/GeoExt/widgets/tree/LayerNode.js

    old new  
    6464        node.visibilityChanging = false; 
    6565    }, 
    6666     
    67     /** private: method[onDestroy] 
     67    /** private: method[destroy] 
    6868     */ 
    69     onDestroy: function() { 
     69    destroy: function() { 
    7070        delete this.radio; 
    71         GeoExt.tree.LayerNodeUI.superclass.onDestroy.call(this); 
     71        GeoExt.tree.LayerNodeUI.superclass.destroy.call(this); 
    7272    } 
    7373}); 
    7474 
     
    225225     *  state 
    226226     */ 
    227227    addVisibilityEventHandlers: function() { 
    228         this.layer.events.register("visibilitychanged", this, function() { 
    229             if(!this.visibilityChanging && 
    230                     this.attributes.checked != this.layer.getVisibility()) { 
    231                 this.getUI().toggleCheck(this.layer.getVisibility()); 
    232             } 
    233         }); 
     228        this.layer.events.on({ 
     229            "visibilitychanged": this.onLayerVisibilityChanged, 
     230            scope: this 
     231        });  
    234232        this.on({ 
    235             "checkchange": function(node, checked) { 
    236                 if (checked && this.layer.isBaseLayer && this.layer.map) { 
    237                     this.layer.map.setBaseLayer(this.layer); 
    238                 } 
    239                 this.layer.setVisibility(checked); 
    240             }, 
     233            "checkchange": this.onCheckChange, 
    241234            scope: this 
    242235        }); 
    243236    }, 
    244237     
     238    /** private: method[onLayerVisiilityChanged 
     239     *  handler for visibilitychanged events on the layer 
     240     */ 
     241    onLayerVisibilityChanged: function() { 
     242        if(!this.visibilityChanging && 
     243                this.attributes.checked != this.layer.getVisibility()) { 
     244            this.getUI().toggleCheck(this.layer.getVisibility()); 
     245        } 
     246    }, 
     247     
     248    /** private: method[onCheckChange] 
     249     *  :param node: ``GeoExt.tree.LayerNode`` 
     250     *  :param checked: ``Boolean`` 
     251     *  handler for checkchange events  
     252     */ 
     253    onCheckChange: function(node, checked) { 
     254        if (checked && this.layer.isBaseLayer && this.layer.map) { 
     255            this.layer.map.setBaseLayer(this.layer); 
     256        } 
     257        this.layer.setVisibility(checked); 
     258    }, 
     259     
    245260    /** private: method[addStoreEventHandlers] 
    246261     *  Adds handlers that make sure the node disappeares when the layer is 
    247262     *  removed from the store, and appears when it is re-added. 
    248263     */ 
    249264    addStoreEventHandlers: function() { 
    250265        this.layerStore.on({ 
    251             "add": function(store, records, index) { 
    252                 var l; 
    253                 for(var i=0; i<records.length; ++i) { 
    254                     l = records[i].get("layer"); 
    255                     if(this.layer == l) { 
    256                         this.getUI().show(); 
    257                     } else if (this.layer == l.name) { 
    258                         // layer is a string, which means the node has not yet 
    259                         // been rendered because the layer was not found. But 
    260                         // now we have the layer and can render. 
    261                         this.render(bulkRender); 
    262                         return; 
    263                     } 
    264                 } 
    265             }, 
    266             "remove": function(store, record, index) { 
    267                 if(this.layer == record.get("layer")) { 
    268                     this.getUI().hide(); 
    269                 } 
    270             }, 
     266            "add": this.onStoreAdd, 
     267            "remove": this.onStoreRemove, 
    271268            scope: this 
    272269        }); 
    273270    }, 
    274271     
     272    /** private: method[onStoreAdd] 
     273     *  :param store: ``Ext.data.Store`` 
     274     *  :param records: ``Array(Ext.data.Record)`` 
     275     *  :param index: ``Nmber`` 
     276     *  handler for add events on the store  
     277     */ 
     278    onStoreAdd: function(store, records, index) { 
     279        var l; 
     280        for(var i=0; i<records.length; ++i) { 
     281            l = records[i].get("layer"); 
     282            if(this.layer == l) { 
     283                this.getUI().show(); 
     284            } else if (this.layer == l.name) { 
     285                // layer is a string, which means the node has not yet 
     286                // been rendered because the layer was not found. But 
     287                // now we have the layer and can render. 
     288                this.render(bulkRender); 
     289                return; 
     290            } 
     291        } 
     292    }, 
     293     
     294    /** private: method[onStoreRemove] 
     295     *  :param store: ``Ext.data.Store`` 
     296     *  :param record: ``Ext.data.Record`` 
     297     *  :param index: ``Nmber`` 
     298     *  handler for remove events on the store  
     299     */ 
     300    onStoreRemove: function(store, record, index) { 
     301        if(this.layer == record.get("layer")) { 
     302            this.getUI().hide(); 
     303        } 
     304    }, 
     305     
    275306    /** private: method[addChildNodes] 
    276307     *  Calls the add method of a node type configured as ``childNodeType`` 
    277308     *  to add children. 
     
    282313        } else if(typeof this.childNodeType.add === "function") { 
    283314            this.childNodeType.add(this); 
    284315        } 
     316    }, 
     317     
     318    /** private: method[destroy] 
     319     */ 
     320    destroy: function() { 
     321        this.layer.events.un({ 
     322            "visibilitychanged": this.onLayerVisibilityChanged, 
     323            scope: this 
     324        }); 
     325        delete this.layer; 
     326        this.layerStore.un("add", this.onStoreAdd, this); 
     327        this.layerStore.un("remove", this.onStoreRemove, this); 
     328        delete this.layerStore; 
     329        this.un("checkchange", this.onCheckChange, this); 
     330 
     331        GeoExt.tree.LayerNode.superclass.destroy.call(this); 
    285332    } 
    286333}); 
    287334