[Mapbender-dev] synchronisation / register functions

Marc Jansen jansen.marc at gmx.de
Mon May 21 07:28:22 EDT 2007


Hi List,

if (typeof(aFunction) == 'function') {
  aFunction();
}

Does this work? As for parameters what about

functionName.call(this, argument1, argument2, ... arguimentN);
(I'm not sure how different user agents interpret the call())

See: 
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:call

-- Marc



Christoph Baudson (WhereGroup) schrieb:
> 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
> _______________________________________________
> Mapbender_dev mailing list
> Mapbender_dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapbender_dev
>


More information about the Mapbender_dev mailing list