Versions Compared

Key

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

Swiz

supports

the

use

of

the

Command

pattern

with

the

{{

CommandMap

}}

bean.

To

use

the

{{

CommandMap

}}

,

first

create

a

command

class:

{code:as3}

Column
Wiki Markup
Code Block
as3
as3
public class MyCommand implements ICommand
{
	public function execute() : void
	{
		trace( "MyCommand.execute() called." );
	}
}
{code:as3}
\\

Your {{


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:

{code:as3}

Code Block
as3
as3
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:as3}
\\

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

{code:as3}


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

Code Block
as3
as3
public class MyCommandMap extends CommandMap
{
	override protected function mapCommands() : void
	{
		mapCommand( MyEvent.MY_EVENT_TYPE, MyCommand, MyEvent );
	}
}
{code:as3}
\\

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:

{code:xml}


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:

Code Block
xml
xml
<swiz:BeanProvider>
	<command:MyCommandMap id="myCommandMap" />
</swiz:BeanProvider>
{code:xml}
\\

As 


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.

Column
width15%