From 7d6f4dea78376bfc4f115e4ff14c7372c4a5429a Mon Sep 17 00:00:00 2001 From: euzuro Date: Tue, 4 Jul 2006 12:44:29 +0000 Subject: [PATCH] changing functionality of Events.register(). Now if a null callback is passed in, no action is taken. If a null *obj* is passed in, however, the obj used is this.object (the Events Object's related object). Added tests to make sure this works. git-svn-id: http://svn.openlayers.org/trunk/openlayers@869 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Events.js | 19 ++++++++++++------- tests/test_Events.html | 14 +++++++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/OpenLayers/Events.js b/lib/OpenLayers/Events.js index d0139b16ca..4e7192f09f 100644 --- a/lib/OpenLayers/Events.js +++ b/lib/OpenLayers/Events.js @@ -77,8 +77,11 @@ OpenLayers.Events.prototype = { /** * @param {String} type Name of the event to register - * @param {Object} obj The object to bind the context to for the callback# - * @param {Function} func The callback function + * @param {Object} obj The object to bind the context to for the callback#. + * If no object is specified, default is the Events's + * 'object' property. + * @param {Function} func The callback function. If no callback is + * specified, this function does nothing. * * #When the event is triggered, the 'func' function will be called, in the * context of 'obj'. Imagine we were to register an event, specifying an @@ -96,12 +99,14 @@ OpenLayers.Events.prototype = { * */ register: function (type, obj, func) { - if (func == null) { - obj = this.object; - func = obj; + + if (func != null) { + if (obj == null) { + obj = this.object; + } + var listeners = this.listeners[type]; + listeners.push( {obj: obj, func: func} ); } - var listeners = this.listeners[type]; - listeners.push( {obj: obj, func: func} ); }, /** diff --git a/tests/test_Events.html b/tests/test_Events.html index c947867130..9fbeb4f682 100644 --- a/tests/test_Events.html +++ b/tests/test_Events.html @@ -41,7 +41,7 @@ function test_02_Events_register(t) { - t.plan( 6 ); + t.plan( 10 ); var mapDiv = $('map'); var obj = {result: 0}; @@ -65,6 +65,18 @@ t.ok( listenerList[1].obj == obj, "obj property correctly registered"); t.ok( listenerList[1].func == func2, "func property correctly registered"); + var func3 = function () { this.result = this.result + 3; } + events.register( "doThingA", null, func3 ); + + var listenerList = events.listeners["doThingA"]; + t.eq( listenerList.length, 3, "register correctly appends new callback to event.listeners[doThingA] even when obj passed in is null" ); + t.ok( listenerList[2].obj == obj, "obj is correctly set to Events.object default when null is passed in."); + t.ok( listenerList[2].func == func3, "func property correctly registered"); + + events.register( "doThingA", obj, null); + + 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" ); }