Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose layer tree node event listeners. #357

Merged
merged 1 commit into from
Sep 2, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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