| Package | com.adobe.cairngorm.control |
| Class | public class FrontController |
The Front Controller is the centralised request handling class in a Cairngorm application. Throughout the application architecture are scattered a number of CairngormEventDispatcher.getInstance().dispatchEvent( event ) method calls, that signal to the listening controller that a user gesture has occured.
The role of the Front Controller is to first register all the different events that it is capable of handling against worker classes, called command classes. On hearing an application event, the Front Controller will look up its table of registered events, find the appropriate command for handling of the event, before dispatching control to the command by calling its execute() method.
The Front Controller is a base-class that listen for events dispatched by CairngormEventDispatcher. In a Cairngorm application, the developer should create a class that extends the FrontController, and in the constructor of their application specific controller, they should make numerous calls to addCommand() to register all the expected events with application specific command classes.
Consider a LoginController, that is the main controller for a Login application that has 2 user gestures - Login and Logout. The application will have 2 buttons, "Login" and "Logout" and in the click handler for each button, one of the following methods is executed:
public function doLogin() : void
{
var event : LoginEvent = new LoginEvent( username.text, password.text );
CairngormEventDispatcher.getInstance.dispatchEvent( event );
}
public function doLogout() : void
{
var event : LogoutEvent = new LogoutEvent();
CairngormEventDispatcher.getInstance.dispatchEvent( event );
}
We would create LoginController as follows:
class LoginController extends com.adobe.cairngorm.control.FrontController
{
public function LoginController()
{
initialiseCommands();
}
public function initialiseCommands() : void
{
addCommand( EVENT_LOGIN, LoginCommand );
addCommand( EVENT_LOGOUT, LogoutCommand );
}
private static const EVENT_LOGIN : String = "login";
private static const EVENT_LOGOUT : String = "logout";
}
In our concrete implementation of a FrontController, LoginController, we register the 2 events that are expected for broadcast - login and logout - using the addCommand() method of the parent FrontController class, to assign a command class to each event.
Adding a new use-case to a Cairngorm application is as simple as registering the event against a command in the application Front Controller, and then creating the concrete command class.
The concrete implementation of the FrontController, LoginController, should be created once and once only (as we only want a single controller in our application architecture). Typically, in our main application, we would declare our FrontController child class as a tag, which should be placed above any tags which have a dependency on the FrontController
<mx:Application xmlns:control="com.domain.project.control.LoginController" ... >
<control:LoginController id="controller" />
...
See also
| Method | Defined by | ||
|---|---|---|---|
|
addCommand(commandName:String, commandRef:Class):void
Registers a ICommand class with the Front Controller, against an event name
and listens for events with that name.
| FrontController | ||
| Method | Defined by | ||
|---|---|---|---|
|
executeCommand(event:CairngormEvent):void
| FrontController | ||
|
getCommand(commandName:String):Class
Returns the command class registered with the command name.
| FrontController | ||
| addCommand | () | method |
public function addCommand(commandName:String, commandRef:Class):voidRegisters a ICommand class with the Front Controller, against an event name and listens for events with that name.
When an event is broadcast that matches commandName, the ICommand class referred to by commandRef receives control of the application, by having its execute() method invoked.
ParameterscommandName:String — The name of the event that will be broadcast by the
when a particular user gesture occurs, eg "login"
|
|
commandRef:Class — An ICommand Class reference upon which execute()
can be called when the Front Controller hears an event broadcast with
commandName. Typically, this argument is passed as "LoginCommand"
or similar.
|
| executeCommand | () | method |
| getCommand | () | method |
protected function getCommand(commandName:String):ClassReturns the command class registered with the command name.
ParameterscommandName:String |
Class |