My intended purpose for the AbstractCommand class in Cairngorm was two-fold:
Batch Commands | Abstract Events | Abstract Delegates

Here’s an example Command in my system, it extends AbstractCommand and implements the ICustomCommand interface – which simply enforces a commandSuccess() and commandFault() method be present.
When the execute() method is invoked, the callback function property of the domain event is assigned by calling the setCallbackFunction() of the AbstractCommand, then the standard process of instantiating the proper delegate happens.
Now that the Command has a reference to the callback function (if it exists), in the commandSuccess() method, we simply call the AbstractCommand’s notifyCaller() method which executes that function.
package business.commands
{
public class DeactivateProjectCommand extends AbstractCommand implements ICustomCommand
{
private var _model:ModelLocator = ModelLocator.getInstance();
public function execute(event:CairngormEvent):void
{
setCallbackFunction((event as ProjectEvent).callbackFunction);
var delegate:ProjectDelegate = new ProjectDelegate();
delegate.setResponder(new Responder(commandSuccess, commandFault));
delegate.deactivate((event as ProjectEvent).eventData);
}
public function commandSuccess(event:ResultEvent):void
{
if (event.result.success)
{
notifyCaller(event.result.data.project as Project);
}
}
public function commandFault(event:FaultEvent):void { }
}
}
This class is straightforward and simple. It simply contains the setCallbackFunction() and notifyCaller() methods with a private class member _callback.
package business.commands
{
public class AbstractCommand
{
private var _callback:Function = null;
public function setCallbackFunction(value:Function):void
{
if (value != null) _callback = value;
}
public function notifyCaller(info:Object = null):void
{
if (_callback != null)
{
if (info != null)
{
_callback.call(this, info);
} else {
_callback.call(this);
}
}
}
}
}
This interface simply enforces the implementation of the commandSuccess() and commandFault() methods.
package business.commands
{
import com.adobe.cairngorm.commands.ICommand;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
public interface ICustomCommand extends ICommand
{
function commandSuccess(event:ResultEvent):void;
function commandFault(event:FaultEvent):void;
}
}
3 Responses for "Cairngorm Patterns: Abstract Commands"
This seems like a really good solution. I’ve been using PropertyChange Events to trigger function calls. Your solution seems a lot easier and cleaner. Thanks.
[...] { import com.widget.thisApp.model.ModelLocator; public class ModuleLoaderCommand extends AbstractCommand implements ICustomCommand { … public function commandSuccess(event:Event):void { if(_component is [...]
_callback.call(this, info);
Do we have to create a function in view component for handling this function ?
Leave a reply