fixed functionality of unregister() function, added ability to pass null value for obj. Added tests.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@870 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2006-07-04 12:57:34 +00:00
parent 7d6f4dea78
commit abac8a5d44
2 changed files with 30 additions and 3 deletions

View File

@@ -115,9 +115,12 @@ OpenLayers.Events.prototype = {
* @param {Function} func
*/
unregister: function (type, obj, func) {
if (obj == null) {
obj = this.object;
}
var listeners = this.listeners[type];
for (var i = 0; i < listeners.length; i++) {
if (listeners[i].obj == obj && listeners[i].type == type) {
if (listeners[i].obj == obj && listeners[i].func == func) {
listeners.splice(i, 1);
break;
}

View File

@@ -39,9 +39,9 @@
"init of Events with null object/element/eventTypes still creates listeners array" );
}
function test_02_Events_register(t) {
function test_02_Events_register_unregister(t) {
t.plan( 10 );
t.plan( 13 );
var mapDiv = $('map');
var obj = {result: 0};
@@ -78,6 +78,30 @@
var listenerList = events.listeners["doThingA"];
t.eq( listenerList.length, 3, "register correctly does not append null callback to event.listeners[doThingA] even when obj passed in is null" );
events.unregister("doThingA", obj, null);
var listenerList = events.listeners["doThingA"];
t.eq( listenerList.length, 3, "trying to unregister a null callback does nothing -- but doesnt crash :-)" );
events.unregister("doThingA", obj, func);
var found = false;
for (var i = 0; i < listenerList.length; i++) {
var listener = listenerList[i];
if (listener.obj == obj && listener.func == func) {
found = true;
}
}
t.ok( (listenerList.length == 2) && !found, "unregister correctly removes callback" );
events.unregister("doThingA", null, func3);
var found = false;
for (var i = 0; i < listenerList.length; i++) {
var listener = listenerList[i];
if (listener.obj == obj && listener.func == func) {
found = true;
}
}
t.ok( (listenerList.length == 1) && !found, "unregister correctly removes callback when no obj specified" );
}