Adding explicit eventListener unregistering in map, layer, and control destroy methods. Note that this only makes explicit what is already happening elsewhere, but it makes us feel complete. Also throwing in the example I missed in the previous change. r=euzuro (closes #1404)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@6447 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-03-06 18:24:18 +00:00
parent 21c097a297
commit e7b9a9a41b
4 changed files with 167 additions and 0 deletions

154
examples/events.html Normal file
View File

@@ -0,0 +1,154 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>OpenLayers Event Handling</title>
<style type="text/css">
#map {
width: 512px;
height: 256px;
border: 1px solid gray;
}
#panel {
margin: 5px;
height: 30px;
width: 200px;
}
#panel div {
float: left;
margin-left: 5px;
width: 25px;
height: 25px;
border: 1px solid gray;
}
#output {
position: absolute;
left: 550px;
top: 40px;
width: 350px;
height: 400px;
}
div.blueItemInactive {
background-color: #aac;
}
div.blueItemActive {
background-color: #33c;
}
div.orangeItemInactive {
background-color: #ca6;
}
div.orangeItemActive {
background-color: #ea0;
}
div.greenItemInactive {
background-color: #aca;
}
div.greenItemActive {
background-color: #3c3;
}
</style>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
var map, panel;
function init(){
// define custom map event listeners
function mapEvent(event) {
log(event.type);
}
function mapBaseLayerChanged(event) {
log(event.type + " " + event.layer.name);
}
function mapLayerChanged(event) {
log(event.type + " " + event.layer.name + " " + event.property);
}
map = new OpenLayers.Map('map', {
eventListeners: {
"moveend": mapEvent,
"zoomend": mapEvent,
"changelayer": mapLayerChanged,
"changebaselayer": mapBaseLayerChanged
}
});
panel = new OpenLayers.Control.Panel(
{div: document.getElementById("panel")}
);
// define custom event listeners
function toolActivate(event) {
log("activate " + event.object.displayClass);
}
function toolDeactivate(event) {
log("deactivate " + event.object.displayClass);
}
// Multiple objects can share listeners with the same scope
var toolListeners = {
"activate": toolActivate,
"deactivate": toolDeactivate
};
var blue = new OpenLayers.Control({
type: OpenLayers.Control.TYPE_TOGGLE,
eventListeners: toolListeners,
displayClass: "blue"
});
var orange = new OpenLayers.Control({
type: OpenLayers.Control.TYPE_TOGGLE,
eventListeners: toolListeners,
displayClass: "orange"
});
var green = new OpenLayers.Control({
type: OpenLayers.Control.TYPE_TOGGLE,
eventListeners: toolListeners,
displayClass: "green"
});
// add buttons to a panel
panel.addControls([blue, orange, green]);
map.addControl(panel);
var vmap = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}
);
var landsat = new OpenLayers.Layer.WMS(
"NASA Global Mosaic",
"http://t1.hypercube.telascience.org/cgi-bin/landsat7",
{layers: "landsat7"}
);
var nexrad = new OpenLayers.Layer.WMS(
"Nexrad",
"http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
{layers:"nexrad-n0r-wmst", transparent: "TRUE", format: 'image/png'},
{isBaseLayer: false}
);
map.addLayers([vmap, landsat, nexrad]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.zoomToMaxExtent();
}
function log(msg) {
document.getElementById("output").innerHTML += msg + "\n";
}
</script>
</head>
<body onload="init()">
<h1 id="title">Event Handling</h1>
<div id="tags">
</div>
<p id="shortdesc">
Demonstrating various styles of event handling in OpenLayers.
</p>
<div id="map"></div>
<div id="panel"></div>
<textarea id="output"></textarea>
<div id="docs"></div>
</body>
</html>

View File

@@ -178,9 +178,14 @@ OpenLayers.Control = OpenLayers.Class({
*/ */
destroy: function () { destroy: function () {
if(this.events) { if(this.events) {
if(this.eventListeners) {
this.events.un(this.eventListeners);
}
this.events.destroy(); this.events.destroy();
this.events = null; this.events = null;
} }
this.eventListeners = null;
// eliminate circular references // eliminate circular references
if (this.handler) { if (this.handler) {
this.handler.destroy(); this.handler.destroy();

View File

@@ -315,8 +315,12 @@ OpenLayers.Layer = OpenLayers.Class({
this.options = null; this.options = null;
if (this.events) { if (this.events) {
if(this.eventListeners) {
this.events.un(this.eventListeners);
}
this.events.destroy(); this.events.destroy();
} }
this.eventListeners = null;
this.events = null; this.events = null;
}, },

View File

@@ -545,6 +545,10 @@ OpenLayers.Map = OpenLayers.Class({
} }
this.viewPortDiv = null; this.viewPortDiv = null;
if(this.eventListeners) {
this.events.un(this.eventListeners);
this.eventListeners = null;
}
this.events.destroy(); this.events.destroy();
this.events = null; this.events = null;