update triggerEvent() so that it doesnt bomb if user tries to trigger a non-enabled event. Added thorough testing.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@875 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2006-07-04 13:54:04 +00:00
parent 91f17f20ff
commit 076632122b
2 changed files with 49 additions and 10 deletions

View File

@@ -158,15 +158,22 @@ OpenLayers.Events.prototype = {
// execute all callbacks registered for specified type
var listeners = this.listeners[type];
for (var i = 0; i < listeners.length; i++) {
var callback = listeners[i];
// use the 'call' method to bind the context to callback.obj
var continueChain = callback.func.call(callback.obj, evt);
if ((continueChain != null) && (continueChain == false)) {
// if callback returns false, execute no more callbacks.
break;
if (listeners != null) {
for (var i = 0; i < listeners.length; i++) {
var callback = listeners[i];
var continueChain;
if (callback.obj != null) {
// use the 'call' method to bind the context to callback.obj
continueChain = callback.func.call(callback.obj, evt);
} else {
continueChain = callback.func(evt);
}
if ((continueChain != null) && (continueChain == false)) {
// if callback returns false, execute no more callbacks.
break;
}
}
}
},

View File

@@ -4,6 +4,7 @@
<script type="text/javascript"><!--
var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
var map;
var a;
function test_01_Events_constructor (t) {
var mapDiv = $('map');
@@ -151,7 +152,7 @@
function test_04_Events_triggerEvent(t) {
t.plan( 2 );
t.plan( 6 );
var mapDiv = $('map');
var obj = {result: 0};
@@ -168,6 +169,37 @@
events.triggerEvent("doThingA");
t.eq( obj.result, 2, "result is 2 after we call triggerEvent with no event" );
var funcB = function() { this.FUNK = "B"; return false; }
events.register( "doThingA", obj, funcB);
events.triggerEvent("doThingA");
t.ok ((obj.result == 3) && (obj.FUNK == "B"), "executing multiple callbacks works")
var funcZ = function() { this.FUNK = "Z"; }
events.register( "doThingA", obj, funcZ);
events.triggerEvent("doThingA");
t.ok ((obj.result == 4) && (obj.FUNK == "B"), "executing multiple callbacks works, and when one returns false, it stops chain correctly")
var func2 = function() { this.result = this.result + 10; }
events.register( "doThingB", null, func2);
events.triggerEvent("doThingB");
t.eq( obj.result, 14, "passing a null object default gets set correctly");
//no specific t.ok for this one, but if it breaks, you will know it.
events.triggerEvent("chicken");
events = new OpenLayers.Events(null, mapDiv, eventTypes);
// a is global variable (context-irrelevant)
a = 0;
var func = function () { a = 5; }
events.register( "doThingC", null, func );
events.triggerEvent("doThingC");
t.eq(a, 5, "if Events has no object set and an event is registered also with no object, triggerEvent() calls it without trying to set the context to null");
}
// -->