The intended purpose for the AbstractEvent class in Cairngorm was three-fold:
Abstract Commands | Abstract Delegates
To accomplish these 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, callbacks:Array=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 callbacks:Array = null;
public function AbstractEvent(type:String, data:Object = null, callbacks:Array=[])
{
super(type);
this.data = data;
this.callbacks = callbacks;
}
}
}
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 thisProject:Project = gridProjects.selectedItem as Project;
new ProjectEvent(ProjectEvent.DEACTIVATE_PROJECT,
{project:thisProject},
[loadProjects]).dispatch();
}
public function loadProjects():void
{
new ProjectEvent(ProjectEvent.LOAD_PROJECTS).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 different 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);
}
}
}
One Response for "Cairngorm: Abstract Events Updated"
Hi Steve,
Quite Informative article. Thanks for sharing :)
Some suggestions:
1) The link ‘Abstract Commands’ provided under the ‘Related Posts’ section proints to the same page. Please correct.
2) The variable declaration for data is missing inside the code snippet given for ‘AbstractEvent.as’
Regards.
Leave a reply