Chapter 6. The CommandMap in action

Earlier in this book, we described a command as being a ‘snack-sized controller’. In any meaningful application the developer has to find ways to tie the important players together—a click on a view might require a service to load some data, from which a model is updated (if there are no errors), and so on. This ‘tying together’ is the realm of the controller code—and it’s usually the most complex and brittle code in your application. The CommandMap and the Command class exist to achieve this ‘tying together’ in a way that is less complex and less brittle than the typical controller approach.

A Command is a concise single-purpose controller object

The Command Pattern is a strategy for separating your controller logic into small self-contained pieces. Instead of having a monolithic MosaicController, we can separate the logic for the Mosaic application into self-describing, single-purpose classes:

Example 6-1. MosaicTool: SelectTileSupplyCommand.as
public class SelectTileSupplyCommand extends Command
{
    [Inject]
    public var tileSuppliesModel:ITileSuppliesModel;

    [Inject]
    public var tileSupplyEvent:TileSupplyEvent;

    override public function execute():void
    {
        tileSuppliesModel.selectedSupplyID = tileSupplyEvent.id;
    } 
}

Commands are triggered by events

In Robotlegs, a command is usually fired in response to an Event. You specify which event you’d like to trigger which command, and then the command map waits for that event to happen and then runs the command. ...

Get ActionScript Developer's Guide to Robotlegs now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.