diff --git a/lib/OpenLayers/Events.js b/lib/OpenLayers/Events.js index 9fcfadfcda..6b05ccbae5 100644 --- a/lib/OpenLayers/Events.js +++ b/lib/OpenLayers/Events.js @@ -111,7 +111,7 @@ OpenLayers.Events.prototype = { /** * @param {String} type - * @param {Object} obj + * @param {Object} obj If none specified, defaults to this.object * @param {Function} func */ unregister: function (type, obj, func) { @@ -127,11 +127,15 @@ OpenLayers.Events.prototype = { } }, - /** + /** Remove all listeners for a given event type. If type is not registered, + * does nothing. + * * @param {String} type */ remove: function(type) { - this.listeners[type].pop(); + if (this.listeners[type] != null) { + this.listeners[type] = new Array(); + } }, /** Trigger a specified registered event diff --git a/tests/test_Events.html b/tests/test_Events.html index 0c059d0ee4..504fff2d3a 100644 --- a/tests/test_Events.html +++ b/tests/test_Events.html @@ -105,7 +105,34 @@ } - function test_03_Events_triggerEvent(t) { + function test_03_Events_remove(t) { + + t.plan( 2 ); + + var mapDiv = $('map'); + var obj = {result: 0}; + var eventTypes = ["doThingA", "doThingB", "doThingC"]; + + events = new OpenLayers.Events(obj, mapDiv, eventTypes); + + var func = function () { this.result++ } + var func2 = function () { this.result-- } + var func3 = function () { this.result = this.result + 3; } + + events.register( "doThingA", obj, func ); + events.register( "doThingA", obj, func2 ); + events.register( "doThingA", null, func3 ); + + events.remove("doThingA"); + + t.eq( events.listeners["doThingA"].length, 0, "remove() correctly clears the event listeners" ); + + events.remove("chicken"); + t.ok( events.listeners["chicken"] == null, "remove on non-enabled event does not break or accidentally enable the event"); + + } + + function test_04_Events_triggerEvent(t) { t.plan( 2 );