Class: ControllerManager

mmir. ControllerManager

new 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

privatecontrollersDictionary

Array of controller-instances

Methods

private_init(callback, ctx){Promise}

Initialize ControllerManager: Load all Controllers from /controller that are specified in /config/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:
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:
info-object: if a path for the generated file exists, a property genPath (String) with the path as value is added.

privatefirstToUpperCase()

HELPER FUNC: convert first letter to upper case

get(ctrlName){Object}

This function gets the controller by name.
Name Type Description
ctrlName String Name of the controller which should be returned
Returns:
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 /config/directories.json, i.e. mmir.CommonUtils#getDirectoryStructure)
Returns:
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/views/application/login.js"
    },
    {
      "name": "registration",
      "path": "views/application/registration.ehtml",
      "genPath": "gen/views/application/registration.js"
    },
    {
      "name": "welcome",
      "path": "views/application/welcome.ehtml",
      "genPath": "gen/views/application/welcome.js"
    }
  ],
  "partials": [
    {
      "name": "languageMenu",
      "path": "views/application/~languageMenu.ehtml",
      "genPath": "gen/views/application/~languageMenu.js"
    }
  ],
  "helper": {
    "fileName": "applicationHelper",
    "name": "ApplicationHelper",
    "path": "helpers/applicationHelper.js"
  },
  "layout": {
    "fileName": "application",
    "name": "application",
    "path": "views/layouts/application.ehtml",
    "genPath": "gen/views/layouts/application.js"
  }
}
//NOTE 1: genPath is an optional field, i.e. it will only be added
          if the corresponding file exists
//NOTE 2: layout may be NULL
This function returns names of all loaded controllers.
Returns:
of all loaded controllers

init(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:
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:
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:
return object of the performed action

privateremoveFileExt()

HELPER FUNC: remove file extension from file-name