Skip to main content

3) Adding functions that change the data and the views

CREATE AN EVENT
PACKAGE DATA IN THE EVENT
CREATE A COMMAND
LINK EVENT TO COMMAND IN CONTROL
DISPATCH EVENT FROM THE VIEW


3.1) For each function in the view (for example, clicking the Submit button on a form), create an event (extending CairngormEvent). The event must extend CairngormEvent, and include a constructor of this form:

public function DataChangeEvent()
{
super( AppController.DATACHANGE_EVENT )

}
3.2) The event will act as a request for some action to occur (for example, LoginUser), or a kind of message between the view (and the user) and the command that will process the event. We can package data of any type within the event, passing it from the view to the command. For example:

public var newMessage : String;
public function DataChangeEvent( newMessage:String )
{
super( AppController.DATACHANGE_EVENT );
this.newMessage = newMessage;
}

3.3) Create a command that will do the work requested by the event, processing its message and altering views and models. The command must contain a reference to AppModelLocator:

private var model : AppModelLocator = AppModelLocator.getInstance();

3.4) Link the event to its command in the controller class, so that the event triggers the correct command and passes its data to it. For example:

public static const DATACHANGE_EVENT : String = "DATACHANGE_EVENT";
public function AppController()
{
addCommand( AppController.DATACHANGE_EVENT, DataChangeCommand );
}


3.5) Add code to the view that will create and despatch an instance of the event (with required data packaged) when the user selects a function. For example, this is the handler for a button click:

private function translate():void
{
model.enableView = false;
var cgEvent : DataChangeEvent = new DataChangeEvent("Dumelang");
CairngormEventDispatcher.getInstance().dispatchEvent( cgEvent );
}
This could also include validation.