[Mapbender-dev] synchronisation / register functions

Christoph Baudson (WhereGroup) christoph.baudson at wheregroup.com
Mon May 21 06:36:55 EDT 2007


To synchronise the modules loaded in a GUI, map.php offers register 
functions like

function mb_registerInitFunctions(stringFunction){
     mb_InitFunctions[mb_InitFunctions.length] = stringFunction;
}

A function registers itself via

mb_registerInitFunctions('tab_init()');


These functions will be executed at certain events, for example onload

function init(){
     for(var i=0; i<mb_InitFunctions.length; i++){
         eval(mb_InitFunctions[i]);
     }
  .
  .
  .
}

A problem with this design is, that a string is evaluated. So when you 
include function parameters in the string, the scope of the passed 
variables is dependent on where the eval takes place. Imagine two 
functions (with parameters) from different modules register at the same 
event...where shall the evaluation take place?

The approach is not modular. What would be nice to have? A redesign 
might not be necessary, but desirable for the future. Maybe we can do 
some brainstorming.

I came up with this, but its drawback is, that you can't pass any 
parameters (so it's kind of pointless).

var FunctionArray = function() {
	this.list = [];

	this.register = function (aFunction) {
		this.add(aFunction);
	}
	this.execute = function () {
		for (var i = 0; i < this.count(); i++) {
			var aFunction = this.get(i);
			if (typeof(aFunction) == 'function') {
				aFunction();
			}
			else if (typeof(aFunction) == 'string') {
				eval(aFunction);
			}
			else {
				var e = new mb_exception("FunctionArray.execute: Invalid parameter: 
" + aFunction);
			}
		}
	}
	this.remove = function (functionString) {
		var listLength = this.count();
		for (var i = listLength - 1; i >= 0; i--) {
			if (this.get(i) == functionString){
				this.del(i);
			}
		}
	}
}

--
Baudson Christoph
http://www.wheregroup.com


More information about the Mapbender_dev mailing list