Skip to content

Commit

Permalink
Merge pull request #357 from bentrm/layernode
Browse files Browse the repository at this point in the history
Dispose layer tree node event listeners.
  • Loading branch information
marcjansen committed Sep 2, 2015
2 parents da5e491 + 5cf29d3 commit a9efeb1
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions src/GeoExt/tree/LayerNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ Ext.define('GeoExt.tree.LayerNode', {
'GeoExt.Version',
'GeoExt.tree.Util'
],

/**
* Cached map this layer node's layer is associated with.
* @type {OpenLayers.Map}
*
* @private
*/
map: null,

/**
* The init method is invoked after initComponent method has been run for
* the client Component. It performs plugin initialization.
Expand Down Expand Up @@ -81,8 +90,25 @@ Ext.define('GeoExt.tree.LayerNode', {
scope: this
});

if (!layer.alwaysInRange && layer.map) {
layer.map.events.register('moveend', this, this.onMapMoveend);
if (layer.map) {
this.map = layer.map;

// Triggers disposal of event listeners if the removed layer maps
// to this plugins layer node.
// TODO: Find a better way to link into lifecycle of the layer node
// to dispose event listeners. See:
// https://github.com/geoext/geoext2/pull/357
this.map.events.on({
'removelayer': this.onMapRemovelayer,
scope: this
});
}

if (!layer.alwaysInRange && this.map) {
this.map.events.on({
'moveend': this.onMapMoveend,
scope: this
});
}

GeoExt.tree.Util.enforceOneLayerVisible(this.target);
Expand Down Expand Up @@ -114,6 +140,38 @@ Ext.define('GeoExt.tree.LayerNode', {
}
},

/**
* Disposes event handlers that have been added during initialization of plugin.
* TODO: Add tests to make sure this works as expected.
*
* @private
*/
onMapRemovelayer: function(evt) {
var target = this.target,
layer = target.get('layer');

if (evt.layer !== layer) {
return;
}

target.un('afteredit', this.onAfterEdit, this);

layer.events.un({
'visibilitychanged': this.onLayerVisibilityChanged,
scope: this
});

this.map.events.un({
'removelayer': this.onMapRemovelayer,
'moveend': this.onMapMoveend,
scope: this
});

this.map = null;

return true;
},

/**
* handler for map moveend events to determine if node should be
* disabled or enabled
Expand Down

0 comments on commit a9efeb1

Please sign in to comment.