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
This commit is contained in:
euzuro
2006-07-04 12:44:29 +00:00
parent 7671f43bb1
commit 7d6f4dea78
2 changed files with 25 additions and 8 deletions

View File

@@ -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} );
},
/**

View File

@@ -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" );
}