The Controller Unit

libflitter/controller/ControllerUnit

exports:  class ControllerUnit

class ControllerUnit

extends:  libflitter/Unit

Loads controllers from the specified directory and creates an access function for them. It makes these available as the Controller context and calls the next function in the stack under it.

constructor(String controller_directory = './app/controllers')

overrides:  super.constructor()         returns:  this

Initializes the class. Resolves the fully-qualified path from the provided controller_directory and stores it in this.directory.

go(express app, function next)

overrides:  super.go()         returns:  undefined

Iterates over files in this.directory and retrieves the controller classes from them. Stores them in _flit.controllers.definitions in key-value pairs, where the key is the name of the controller and the value is an instance of the class. The names are parsed from the file names and use Flitter's convention for sub-directory naming. Creates a function to access these instances and makes it available at _flit.controllers.controller(). These establish the Controller context and then the next function in the stack is called under it.

provides()

overrides:  super.provides()         returns:  Object

Returns an Object describing the services, templates, and directories provided by this unit. They are as follows:

Type Key Value Notes

Service Name

name

"controllers"

The name of the service. This is added to the _flit.services array so other units can check for this unit.
Directory directories.controllers this.directory The fully-qualified path of the config files. This is added to the _flit.directories store so other units can access it.
Template templates.controller Object Template that creates new config file in this.directory.
directory

type:  String

Set on initialization. Specifies the fully-qualified path of the controller class files to be processed. Each of these files should export a class of request-handling methods. Each file's class is assigned a name based on the name of the file it was retrieved from. This includes sub-directories. As is convention in Flitter, sub-directories are delineated with a : character. So a controller class loaded from the file auth/Login.controller.js would be named auth:Login.

Controller Context

This context provides access to instances of the controller classes by name, as well as the global access function, controller().

_flit.controllers

type:  Object

Holds the variables and functions provided by this unit.

_flit.controllers.definitions

type:  Object

A collection of key-value pairs such that they keys are the names of the controllers parsed from the files in the specified directory. These names follow the Flitter convention for sub-directory naming. The values are instances of the corresponding controller classes.

_flit.controllers.controller(String name)

returns:  mixed

Access function that returns the controller from _flit.controllers.definitions with the corresponding name.

global.controller(String name)

returns:  mixed

Mapped to _flit.controllers.controller().

No Comments
Back to top