CAAT.ModuleManager

The ModuleManager controls module loading operations. Tipically, a module consists of a single JavaScript file, which after being processed by the ModuleManager, will define a Class object.

It has the following features:

  • Solves very deep transitive dependencies.
  • Bundled files (all files concatenaded for production environments)
  • Single file retrieval (get file by file for testing environments)
  • Only javascript files loading
  • Load Mixing of JavaScript file libraries and Modules loading
  • Reentrant notification points.
  • It does not pollute global namespace.
  • Based on John Resig's Class pattern.

The ModuleManager, not only loads modules, and handles its dependencies by loading other modules, and keeping you safe appart from cyclic dependencies, but also loads JavaScript library files, notifies you about modules been solved, etc.

Minimally, a ModuleManager must be configured with a bring( [] ), and an onReady callback function..

A typical ModuleManager definition could be:

                            CAAT.ModuleManager.

                            // define the baseURL, a base URL component for all
                                baseURL("data/src/animation").

                            // define some module
                                setModulePath("CAAT.Module",            "Modules").
                                setModulePath("CAAT.Math",              "Math/v1").
                                setModulePath....

                            // get modules, and solve their dependencies.
                                bring(
                                [
                                  "CAAT.PathUtil.Path",
                                  "CAAT.Foundation.Director",
                                  "CAAT.Foundation.Scene",
                                  "CAAT.Foundation.UI.Layout.BoxLayout",
                                  "CAAT.Module.Preloader.ImagePreloader",
                                  "CAAT.Module.Storage.LocalStorage"
                                ]).

                            // this function will be firer every time all dependencies have been solved.
                            // if you call again bring, this function could be fired again.
                                onReady(function () {

                                    // safe to use these modules and their dependencies here.

                              });
                        

MoMa.ModuleManager.baseURL(path)

The base URL defines a document root to read and solve modules from. The function will append / in case the path does not contain it.

MoMa.ModuleManager.setModulePath(module, path)

Define as much module paths as needed. for example:

                                MoMa.ModuleManager.setModulePath("CAAT.Foundation", "src/Foundation")

                                //will make a module like

                                MoMa.Module({
                                 defines : "CAAT.Foundation.UI.Label",
                                 ...

                                //resolve to

                                src/Foundation/UI/Label.js
                            

MoMa.ModuleManager.bring( ["module1 | .js file", "module2 | .js file",...] )

This functions loads all the modules or libraries specified. Modules are loaded and solved on the fly, while .js files, are simply loaded. When **MoMa** ends loading and solving all files/modules, it notifies via callback to MoMa's **onReady** function. **The onReady function, when invoked, will remove all onReady observers.**

This function loads a file if the array value ends with **js**, or tries to load a module otherwise.

MoMa.ModuleManager.addModuleSolvedListener(modulename,callback)

Add a callback function when a given module has been solved.

MoMa.ModuleManager.load(file, onload, onerror)

Load a js file, and notify callbacks on file load, or on error. This function does not try to solve any module contained in the file. Intended to load independant non-module files.

MoMa.ModuleManager.onReady(callback)

Call the callback funtion when all the files specified by a call to bring have been loaded and solved. It is safe to start any program from this callback function.

MoMa.ModuleManager.status()

Module resolution

The rules to load a module from a call to **bring** are the following:

  • if the module_name ends with .js
    • if starts with /, the module resolves to module_name.substring(1)
    • else the module resolves to **baseURL/module_name
  • else
  • if a suitable modulePath defined by a call to setModulePath exists
    • strip module_path prefix from module_name and change it by the associated path
    • change . by /
    • prepend baseURL
  • else return ModuleManager.baseURL + module.replace(/\./g,"/") + ".js"; (which may fail)

For example:


                               MoMa.ModuleManager.
                                 setBaseURL("/code/js").
                                 setModulePath( "CAAT.Foundation", "src/Foundation" );

                               MoMa.bring( [
                                   "CAAT.Foundation.Actor",
                                   "CAAT.Foundation.UI.Label",
                                   "a.js",
                                   "/a.js"
                               ] );
                            

CAAT.Foundation.Actor will resolve to: /code/js/src/Foundation/Actor.js ( '/code/js/' + 'src/Foundation/' + 'Actor' + '.js')

CAAT.Foundation.UI.Label will resolve to: /code/js/src/Foundation/UI/Label.js ('/code/js/' + 'src/Foundation/' + 'UI/Label' + 'js')

a.js will resolve to: /code/js/a.js ('/code/js' + 'a.js')

/a.js will resolve to: a.js