Simple object module dispatcher. Performs a recursive loop over defined module(s) object properties.
"test: yes" will appear, the other not.
//first, create the ObjectDisatcher instancevarOD=newObjectDispatcher('app');OD.module('index',{//no __init function (notice double underscore) for top-level: checked if module with current moduleId exists//(by default: #module-[moduleId], in this case #module-index)//see the options for more info about prepending/appending to the selectortest: function(){console.log('test: yes');},//starting with "_" - no automatic dispatch (must be invoked manually, e.g. in an __init function)_testNo: function(){console.log('test: no');},//submoduletestSubIndex: {//manual check - if returns truthy value, becomes dispatched__init: function(){returnOD.isModule('index');//checks if an element that matches "#module-index" selector exists},testSubIndex1: function(){console.log('testSubIndex1: yes');}},//submoduletestSub: {//no __init function for submodule - always dispatchedvariable: 1,testSub1: function(){console.log('testSub1: yes');},testSub2: function(){console.log('testSub2: yes');},testSub3: {//if __init function is defined, it must return a truthy value to perform dispatching__init: function(){this._testSub32();},testSub31: function(){console.log('testsub31: no');},_testSub32: function(){console.log('_testSub32: yes');}},testSub4: {__init: function(){returnOD.isModule(['not-exists','not-exists-2']);},testSub41: function(){console.log('testsub41: no');}}},//submodule as a closure with "private" variablestestMod: (function(){varvariable=1;return{testMod1: function(){console.log('testmod1: yes',variable++);},_testMod2: function(){console.log('testmod2: no');},testMod3: function(){console.log('testmod3: yes',variable);}}})(),//layouttestLayout: {//guaranteed to be dispatched only once (useful e.g. for single page apps layout code, common for all views)__once: true,testLayout1: function(){console.log('testLayout1: yes (only once!)');}}});//run dispatcherOD.dispatch();//access a specified method manuallyOD.module('index').testSub.testSub3._testSub32();Assuming HTML:
<divid="module-index">
...
</div>Dispatch can be called at the object creation. If you pass true as the first parameter, it will be internally wrapped in setTimeout(0) to run it at the end of the queue (when all modules are ready). This is useful when you keep module definitions in separate files, and do not know when all of them are already registered.
So, the first line of the example could look like this:
varOD=newObjectDispatcher('app').dispatch(true);Of course, the "run dispatcher" line at the end of the example should be then removed.
The second parameter of the dispatch function will be passed to all __init functions:
OD.dispatch(false,'parameter');//...//module definition{__init: function(data){console.log(data);//outputs "parameter"}}To pass more variables, use an object:
OD.dispatch(false,{par1: 'val1',par2: 'val2'});Pass as a second argument to the constructor.
//shows default optionsvaroptions={//module check optionsisModule: {//pass a function to replace standard behaviorfn: false,//prepended to tested selectorprepend: '#module-',//appended to tested selectorappend: ''}}See more in the source comments.