My intended purpose for the AbstractEvent class in Cairngorm was three-fold:
Abstract Commands | Abstract Delegates
To accomplish my goals, each domain event (e.g. ProjectEvent, PaymentEvent, etc.) would be polymorphic (kinda) in that there is no longer just one identifier designated with the EVENT_ID constant. In addition, I wanted to abstract as much functionality and classification as possible into a common class that each domain event would extend.

Here’s an example of a domain event. It has three public identifiers – CREATE_PROJECT, LOAD_PROJECTS, DEACTIVATE_PROJECT- and has no logic other than to pass its constructor’s arguments to AbstractEvent.
package business.events
{
import com.adobe.cairngorm.control.CairngormEvent;
public class ProjectEvent extends AbstractEvent
{
public static const CREATE_PROJECT:String = "createProject";
public static const LOAD_PROJECTS:String = "getProjects";
public static const DEACTIVATE_PROJECT:String = "deactivateProject";
public function ProjectEvent(type:String, data:Object = null, callback:Function = null)
{
super(type, data, callback);
}
}
}
The AbstractEvent class itself isn’t much more complicated at all. It simply has three public variables to hold the type of the event, an object containing key/value pairs for the data, and the reference to the callback function in the view (if specified).
package business.events
{
import com.adobe.cairngorm.control.CairngormEvent;
public class AbstractEvent extends CairngormEvent
{
public var eventType:String = "";
public var eventData:Object = new Object();
public var callbackFunction:Function = null;
public function AbstractEvent(type:String, data:Object = null, callback:Function = null)
{
super(type);
eventType = type;
eventData = data;
callbackFunction = callback;
}
}
}
Here’s some snippets of code from my project view. One piece of functionality is for a user to disable a selected project from a list in an AdvancedDataGrid. When the user clicks on the “Disable Project” button, the btnDeactivateProject_Click() function fires and creates a new Project event in which the loadProjects() function is specified as the callback function.
private function btnDeactivateProject_Click(e:Event):void
{
var p:Project = gridProjects.selectedItem as Project;
var event:ProjectEvent = new ProjectEvent(
ProjectEvent.DEACTIVATE_PROJECT,
{project:p},
loadProjects);
event.dispatch();
}
public function loadProjects():void
{
var projectEvent:ProjectEvent = new ProjectEvent(ProjectEvent.LOAD_PROJECTS);
projectEvent.dispatch();
}
<mx:Button id="btnDeactivateProject"
click="btnDeactivateProject_Click(event)"
x="10" y="393"
label="Deactivate Project"/>
Your controller logic doesn’t change. The only difference is that you’re using one domain event, with specified identities, mapped to difference Commands.
package business
{
import business.commands.*;
import business.events.*;
import com.adobe.cairngorm.control.FrontController;
public class Controller extends FrontController
{
public function Controller()
{
super();
addCommand(ProjectEvent.LOAD_PROJECTS, LoadProjectsCommand);
addCommand(ProjectEvent.CREATE_PROJECT, CreateProjectCommand);
addCommand(ProjectEvent.DEACTIVATE_PROJECT, DeactivateProjectCommand);
}
}
}
2 Responses for "Cairngorm Patterns: Abstract Events"
Here’s a question. I try to avoid reusing Class names. There is an already an AbstractEvent Class built in to Flex in the mx.rpc.events package. Where does the use of the word “Abstract” come from? Is there some other name that can be given to Abstract Events and Commands?
Here’s the basic concept of creating abstract classes (http://en.wikipedia.org/wiki/Class_(computer_science)#Abstract_classes)
If you truly want to avoid the name conflict to avoid confusion, you can name it BaseEvent, or CoreEvent, or something along those lines if you wish.
Leave a reply