My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Reactions  
Using Reactions
Featured
Updated Aug 9, 2010 by rafael.s...@gmail.com


Introduction

Reactions are the equivalent of Reflexive notification interests for component events. Instead of subscribing to component events manually using addEventListener, you declare your interest in the component event using reactTo<ComponentNameEventName> syntax.

Requirements

This feature needs Fabrication version 0.6 or above.

Usage Scenario

Consider the typical anatomy of a PureMVC Mediator class for an mxml view component show below.

MyForm.mxml

<mx:Form>
	<mx:TextInput id="emailInput" />
	<mx:Button id="submitButton" label="Submit Button"/>
</mx:Form>

FormMediator.as

public class FormMediator extends FlexMediator {

	static public const NAME:String = "FormMediator";

	public function FormMediator(viewComponent:Object) {
		super(NAME, viewComponent);
	}

	public function get submitButton():Button {
		return viewComponent.submitButton as Button;
	}

	public function onRegister():void {
		submitButton.addEventListener(MouseEvent.CLICK, submitButtonClicked);
	}

	private function submitButtonClicked(event:MouseEvent):void {
		// submit form
	}

}

Instead of subscribing to the click event you just add a click handler with the reactTo syntax.

public function reactToSubmitButtonClick(event:MouseEvent):void {
	// submit form
}

Implementation Details

  1. To use Reactions you must declare you target component with a getter in the Mediator using CamelCasing. In the above example the submitButton getter referenced the submitButton child of the viewComponent.
  2. In the reactTo method the submitButton must be upper CamelCased. In the above example submitButton became SubmitButton.
  3. The Component's event name must also be upper CamelCased. In the above example the click event became Click.
  4. If you have any other code implemented in the onRegister method you need to call super.onRegister.
  5. Similarly if you have any other code implemented in the onRemove method you need to call super.onRemove.

Capture Phase

The reactTo methods implement addEventListener with a false useCapture argument. To use the reaction with capture phase, The method prefix should be trap. To react to the click event in the above example in capture phase the handler would be,

public function trapSubmitButtonClick(event:MouseEvent):void {

}

Removing Reactions

One useful aspect of Reactions is that you do not need to use removeEventListener explicitly. Once the mediator is removed its Reactions are removed as well. If you need to remove the reaction manually, use the removeReaction method. The syntax is,

removeReaction(handler:Object):void
  • handler : Can be the string name of the handler to remove or a reference to it.

To remove the handler in the above example use,

// with handler reference
removeReaction(reactToSubmitButtonClick);

// with handler string
removeReaction("reactToSubmitButtonClick");

Examples

  1. InterceptorDemoMediator Browse SVN
  2. SimpleFsmMediator Browse SVN
Comment by gilbuns@gmail.com, May 10, 2009

Maybe worth pointing out that both the getter function and the reactTo function must be declared public.

Comment by czgal...@gmail.com, Nov 10, 2009

Hey all i was wondering what the benefits or back falls might be from using the reactTo instead of the usual AddEventListener?? thanks

Comment by manuraj....@gmail.com, Nov 16, 2009

@czgaljic Benefits: Reducing code as well as making your development cycle faster. "addEventListener" is already done for you here.

Comment by atendime...@gmail.com, Aug 18, 2010

I cant react to this, (the component itself), only his childrens. There´s a way?

Thanks.

Comment by project member rafael.s...@gmail.com, Aug 18, 2010

of course:

public function get viewComp():UIComponent {

return viewComponent as UIComponent;
}

then:

public function reactToViewCompClisk( event:MouseEvent? ):void {

//

}

Comment by atendime...@gmail.com, Aug 19, 2010

Are you meaning that only this way works?

i mean, by comp$event doesnt work only with the "this" ?

Comment by atendime...@gmail.com, Aug 30, 2010

Okay.... sorry posting only today. But ive tried that and it works independently the method.. the new method or not... the problem was too in this case, that i was using old style protected getter for the component, i already changed it to public, i think its good to make this more clear to people dont get confused and less error prone... but now raphal, im running into other problem... i cant react to a custom component! for example, i have a Extended:SpringLoadedTree?... "Extended" is a xmlns namespace for the component.. it just dont react, neither in the old or new method. very very very thanks for maintaining this fabulous project.

Comment by atendime...@gmail.com, Sep 1, 2010

SORRY RAPHAL! it was my mistake... the event name must be exactly..so when i get used with addeventlistener, i´ve got confused the function name was itemselected...!! hah now i´ve changed to itemClick, and it worked. Thanks. sorry by my mistake. it´s working flawless.

Comment by atendime...@gmail.com, Sep 1, 2010

now it´s serious! i can´t get it to work when i´ve instantiated with new keyword...

like private or public, var, component:Component;

then i can´t react. so in this case im using listener.. any clue??? thanks

Comment by atendime...@gmail.com, Sep 1, 2010

i can´t react directly to stage too? :D

Comment by project member rafael.s...@gmail.com, Sep 2, 2010

Hi atendimento,

Can you send me any example on problems you have?

Comment by arnaud.h...@gmail.com, Sep 27, 2010

Hi Rafael,

I'm trying to use the reactTo feature in a pure AS3 project (no flex) but i cant make it work, after many tries and modifs to my code Can you tell me if it's even possible (i wasnt able to find any example for this in pure as3 ). Thanks for answering me

Comment by broadysm...@gmail.com, Dec 11, 2010

Weird, tested it dozen times, and looks like Point 3. in "Implementation details" is wrong:

"The Component's event name must also be upper CamelCased?. In the above example the click event became Click."

When I make a reactTo function like this:

public function reactToTestTextFieldClick(event:MouseEvent?):void { trace('route notification'); }

Then the following works:

public function get testTextField():TextField? { return myShell.tff as TextField?; }

But capital letter won't work:

public function get TestTextField?():TextField? { return myShell.tff as TextField?; }


So looks like You not only can make first letter small, but you also must do it!


Sign in to add a comment
Powered by Google Project Hosting