Class: ControllerManager

mmir. ControllerManager

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.

Members

private,staticmmir.ControllerManager.logger

The logger for the ControllerManager.

privatecontrollersMap

Array of controller-instances

Methods

create ControllerManager instance

private_init(callback, ctx){Promise}

Initialize ControllerManager: Load all Controllers from /controller that are specified in /gen/directories.json
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

privateaddGenPath(genDirPath, infoObj, fileNamePrefix){PlainObject}

HELPER FUNC: add file path for generated / compiled view-element if it exists.
Name Type Description
genDirPath String path the the directory, where the file for the generated view-element is potentially located (generated file may not exists)
infoObj PlainObject the info-object for the view-element. MUST HAVE property name!
fileNamePrefix String optional OPTIONAL prefix for the file-name, e.g. in case of Partials, the file-name would be: fileNamePrefix + infoObj.name (+ file-extension)
Returns:
Type Description
PlainObject the info-object: if a path for the generated file exists, a property genPath (String) with the path as value is added.

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[]

privatefirstToUpperCase()

HELPER FUNC: convert first letter to upper case
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

privategetControllerResources(controllerName, controllerPath){JSON}

This function gets the controller file names and builds a JSON object containing information about the location, file name etc. for the controller itself, its views, partials, layout, and helper.
Name Type Description
controllerName String the name of the Controller (must start with an upper case letter).
controllerPath String the path (URL) where the file with the Controller's implementation is located (according to information in file /gen/directories.json, i.e. mmir.CommonUtils#getDirectoryStructure)
Returns:
Type Description
JSON JSON-Object containing information about the controller, its views, partials, and paths etc.
Example
//EXAMPLE for returned object:
{
  "fileName": "application",
  "name": "Application",
  "path": "controllers/application.js",
  "views": [
    {
      "name": "login",
      "path": "views/application/login.ehtml",
      "genPath": "gen/view/application/login.js"
    },
    {
      "name": "registration",
      "path": "views/application/registration.ehtml",
      "genPath": "gen/view/application/registration.js"
    },
    {
      "name": "welcome",
      "path": "views/application/welcome.ehtml",
      "genPath": "gen/view/application/welcome.js"
    }
  ],
  "partials": [
    {
      "name": "languageMenu",
      "path": "views/application/~languageMenu.ehtml",
      "genPath": "gen/view/application/~languageMenu.js"
    }
  ],
  "helper": {
    "name": "ApplicationHelper",
    "path": "helpers/applicationHelper.js",
    "genPath": "helpers/applicationHelper.js"
  },
  "layout": {
    "name": "application",
    "path": "views/layouts/application.ehtml",
    "genPath": "gen/view/layouts/application.js"
  }
}
//NOTE: layout and helper may be NULL
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

privateprocessFileList(dirPath, genDirPath, queryFilter, removeNamePrefix, regExpFileFilter){Array.<FileInfo>}

HELPER FUNC: parse file list and extract info for view/partial/layout/helper.
Name Type Description
dirPath String path the the directory, where the (source) file is located, e.g. eHTML template or JS helper implementation
genDirPath String path the the directory, where the file for the generated view-element is potentially located (generated file may not exists)
queryFilter QueryFilter the filter for querying (i.e. filtering) commonUtils.listDir(): queryFilter.nameStart: {String} an regular expression that the file-name must match (or empty string, if all file-names should match) queryFilter.ext: {String} the file-extension that should match, with dot e.g. "js" or "ehtml"
removeNamePrefix String optional OPTIONAL prefix of the file-name which should be removed when extracting the InfoObj.name, e.g. in case of Partials: removeFileExt(fileName) === removeNamePrefix + infoObj.name (+ file-extension)
regExpFileFilter RegExp optional OPTIONAL regular expression for filtering the fileList, i.e. only files that match the expression will be included in the returned list of FileInfo objects
Returns:
Type Description
Array.<FileInfo> list of file-info objects: FileInfo.name: {String} the name that will be used to identify the resource FileInfo.path: {String} the path to the source file, e.g. for loading the resource FileInfo.genPath: {String} the path to generated/executable resource

privateremoveFileExt()

HELPER FUNC: remove file extension from file-name