Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Section
Column
width15px

Your execute() method contains whatever logic you want the command to perform. Swiz will automatically set up the command as a Prototype Bean, so you can use [Inject] to inject other beans, use [Dispatcher] to dispatch an event, etc.

In addition to the ICommand interface, Swiz also provides the IEventAwareCommand. Implementing this interface causes the event which triggers the command to be set into your command object. This way, data associated with the event can also be used by the execute() method:

With the command class created, you can now write a CommandMap to associate the command with an event: class, which you then register as a bean. The first step is to extend CommandMap and override the mapCommands() method. Inside the method you make individual calls to mapCommand(), which maps an event to a command class. In the example below, the first two arguments are pretty self explanatory. They are the event type and the command you want to be triggered by that type. The third and fourth arguments are optional, but can be very useful.

The third argument is the event class associated with the event type, and when provided allows Swiz to prevent event type collisions. What this means is that if MyEvent.OPEN and YourEvent.OPEN both evaluate to "open", providing MyEvent as the third argument will instruct Swiz to only execute MyCommand in response to MyEvent.OPEN. This argument is optional but recommended.

The fourth argument is oneTime:Boolean = false. If you set this argument to true, Swiz will remove the mapping after the first time the command is executed. This can be useful for activities you know will only happen once, like configuration loading or other bootstrapping activities.

Column

Swiz supports the use of the Command pattern with the CommandMap bean. To use the CommandMap, first create a command class:

Code Block
as3as3

public class MyCommand implements ICommand
{
	public function execute() : void
	{
		trace( "MyCommand.execute() called." );
	}
}
Code Block
as3as3

public class MyCommand implements IEventAwareCommand
{
	private var _event : MyEvent;

	public function set event( value : Event ) : void
	{
		_event = value as MyEvent;
	}

	public function execute() : void
	{
		trace( "MyCommand.execute() called. Event info: " + _event.toString() );
	}
}
Code Block
as3
as3
public class MyCommandMap extends CommandMap
{
	override protected function mapCommands() : void
	{
		mapCommand( MyEvent.MY_EVENT_TYPEOPEN, MyCommand, MyEvent, false );
	}
}
As you can see, you override the mapCommands() method of CommandMap, and use mapCommand() to associate an event with the command. Whenever a matching event is processed by Swiz, the associated command will be executed.


Finally, add your CommandMap to one of your BeanProviders:. It is not necessary to register your Command classes as beans. Their presence in the mappings is all the information Swiz needs.

Code Block
xml
xml
<swiz:BeanProvider>
	<command:MyCommandMap id="myCommandMap" />
</swiz:BeanProvider>


The commands you register must implement one of two interfaces provided by Swiz. ICommand is the base interface, simply defining an execute() method.

Code Block
as3
as3
public interface ICommand
{
	function execute():void;
}


If your Command needs access to the event that triggered it, simply implement IEventAwareCommand. Notice IEventAwareCommand extends ICommand.

Code Block
as3
as3

public interface IEventAwareCommand extends ICommand
{
	function set event( value:Event ):void;
}


As always, make sure you've added the correct event package containing your event to the Swiz eventPackages array . Thatand that's all there is to it. Any time a view dispatches a bubbling event of that type, or you use the Swiz dispatcher to dispatch that Swiz handles an event, the mapped associated command(s) will runbe executed.

Column
width15%