mmir.ControllerManager
A class for managing the controllers of the application.
It's purpose is to load the controllers and their views / partials and provide functions to find controllers or perform actions or helper-actions. This "class" is structured as a singleton - so that only one instance is in use.
It's purpose is to load the controllers and their views / partials and provide functions to find controllers or perform actions or helper-actions. This "class" is structured as a singleton - so that only one instance is in use.
Methods
-
createCtrlInstance(fileName, result)
-
HELPER create the controller instance after its implementation was loaded/is available in ctx or via argument result After the controller instance was initialized, it is added to ctrlList (with its name as key)
Name Type Description fileName
String the name of the controller's file: application.js -> Controller result
any | function the result of loading the controller implementation, i.e. its constructor function; if undefined, the constructor must be available in ctx in ctx[ ] -
get(ctrlName){mmir.ctrl.Controller}
-
This function gets the controller by name.
Name Type Description ctrlName
String Name of the controller which should be returned Returns:
Type Description mmir.ctrl.Controller controller if found, null else -
This function returns names of all loaded controllers.
Returns:
Type Description Array.<String> Names of all loaded controllers -
asyncinit(callback, ctx){Promise}
-
This function must be called before using the
mmir.ControllerManager
. The Initialization process is asynchronous, because javascript-files must be loaded (the controllers). To ensure that the ControllerManager is initialized, a callback can be used, or the returned Promise (i.e. a "then-able" object) for code, that relies on the presence of the loaded controllers.
Note: The callback function should be used for code, that requires the prior loading of the controllers.
The callback mechanism is necessary, because loading the controllers is asynchronous.
If provided, the callback function is invoked with 1 argument, the ControllerManager instance:
callbackFunction(controllerManagerInstance)
Name Type Description callback
function optional OPTIONAL an optional callback that will be triggered after the controllers where loaded ctx
Object optional OPTIONAL the context for the controller & helper implementations (DEFAULT: the global context, i.e. window) Returns:
Type Description Promise a deferred promise that will get fulfilled when controllers are loaded Example
//recommended style: mmir.require(['mmirf/controllerManager', ...], function(controllerManager, ...) { controllerManager.init().then(function(theInitializedControllerInstance){ ... }); }) //old style: function afterLoadingControllers(controllerManagerInstance){ var appCtrl = controllerManagerInstance.get('Application'); //do something... } mmir.ctrl.init(afterLoadingControllers);
-
perform(ctrlName, actionName, data){Object}
-
This function performs an action of a controller.
Name Type Description ctrlName
String Name of the controller to which the action belongs actionName
String Name of the action that should be performed data
Object optional data that can be submitted to the action Returns:
Type Description Object the return object of the performed action -
performHelper(ctrlName, actionName, data){Object}
-
This function performs an action of a helper-class for a controller.
Name Type Description ctrlName
String Name of the controller to which the helper action belongs actionName
String Name of the action that should be performed by the helper data
Object optional data that can be submitted to the action Returns:
Type Description Object the return object of the performed action