O'Reilly logo

ActionScript Developer's Guide to Robotlegs by Joel Hooks, Stray

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required