The intended purpose for the AbstractCommand class in Cairngorm is three-fold:
Batch Commands | Abstract Events | Abstract Delegates

Below is an example Command in my system, it extends AbstractCommand which in turn implements the ICustomCommand interface. To make the Command responseful, an array of functions is passed from the Event to the Command, which will execute them upon success (see more below). Here’s an example call:
new EmployeeEvent(EmployeeEvent.GET_DETAILS,
{employee_id:params.empID},
[showEmployeeDetails, updateEmployeeGraph])
To implement a very basic command, it is only required to set the delegate property to a new instance of the appropriate Delegate class, and then invoke the needed method. The AbstractCommand will automatically create a new Responder object and assign it to the delegate.
public class LoadProjectCommand extends AbstractCommand
{
private var _model:ModelLocator = ModelLocator.getInstance();
override public function execute():void
{
delegate = new ProjectDelegate();
(delegate as ProjectDelegate).loadProject(invoker.data);
}
override public function commandSuccess(event:Event):void
{
if (event.result.success)
{
_model.project = (event as ResultEvent).result.data;
notifyInvoker();
}
}
}
}
The invoker object is a reference to the original Event that was called, and to get it assigned as a property of the Command, I extended the FrontController class so that it is assigned before the Command is executed.
protected function executeAbstractCommand(event:AbstractEvent):void
{
var commandRef:Class = getCommand(event.type);
var commandToExecute:ICustomCommand = new commandRef();
commandToExecute.invoker = event;
commandToExecute.execute();
}
This class is straightforward and handles the tedious aspects of creating a Command. The notifyInvoker() method simply loops through the array of callback functions – if they exist – and runs them.
public class AbstractCommand
{
private var _callbacks:Array = null;
private var _invoker:AbstractEvent = null;
private var _delegate:AbstractDelegate = null;
public function AbstractCommand() { }
public function execute():void { }
public function commandSuccess(event:Event):void { }
public function commandFault(event:Event):void { }
public function set delegate(d:*):void
{
_delegate = d;
_delegate.setResponder(new Responder(commandSuccess, commandFault));
}
public function get delegate():*
{
return _delegate;
}
public function set invoker(e:AbstractEvent):void
{
_invoker = e;
if (e.callbacks != null) _callbacks = e.callbacks;
}
public function get invoker():AbstractEvent
{
return _invoker;
}
public function notifyInvoker(info:Object = null):void
{
if (_callbacks != null)
{
for (var i:uint = 0; i < _callbacks.length; i++)
{
var callback:Function = _callbacks[i];
(info != null) ? callback.call(this, info) : callback.call(this);
}
}
}
}
package business.commands
{
import business.events.AbstractEvent;
import flash.events.Event;
public interface ICustomCommand
{
function commandSuccess(event:Event):void;
function commandFault(event:Event):void;
function execute():void;
function set invoker(event:AbstractEvent):void;
function set delegate(delegate:*):void;
}
}
3 Responses for "Cairngorm: Abstract Commands Updated"
Seems interesting ! Actually the focus of this purposes is the main reason for me to seek for an alternative to cairngorm… So i’d love to see a beautiful and simple solution emerge. I tried with UniversalMind extension too, but a bit heavy to switch to.
@Ricovitch: I am also using the UM package, but a significant part of their code has either been overwritten or ignored, because I had already implemented similar functionality in a more lightweight manner. In essence, I’m only using their code that involves managing Flex Modules.
One of the good things about Cairngorm is that since it is such a basic code architecture, you can easily extend, override, or simply replace a lot of its code with something better.
This, of course, begs the question “Why use Cairngorm then if I’m simply going to modify it to an extent to which it’s no longer recognizable?” That’s what I’ve been struggling with, but since our corporate standard is Cairngorm, I have to work with what I’m given :)
[...] Back to the problem, I like Cairngorm and use it, but I don’t like the fact of the strict coupling between the ModelLocator and the View. To solve this problem I looked at Mate and how they delt with some of the issues, namely dependancy injection. I started researching a little and came across a few articles that spiked my interest one by Allen Manning and another by Steve Brownlee. [...]
Leave a reply