Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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

 

public class MyCommand implements ICommand
{
public function execute() : void

Unknown macro: { trace( "MyCommand.execute() called." ); }

}

 


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:

 

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

public function set event( value : Event ) : void

Unknown macro: { _event = value as MyEvent; }

public function execute() : void

Unknown macro: { trace( "MyCommand.execute() called. Event info}

}

 


With the command class created, you can now write a CommandMap to associate the command with an event:

 

public class MyCommandMap extends CommandMap
{
override protected function mapCommands() : void

Unknown macro: { mapCommand( MyEvent.MY_EVENT_TYPE, MyCommand, MyEvent ); }

}

 


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:

 

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

 


As always, make sure you've added the correct event package containing your event to the Swiz eventPackages array. 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 event, the mapped command will run.

  • No labels