How to use Instructions and Mapper on your application
In this sample you'll see how to send an instruction from a component and how to listen for it from another one
Even if in this sample we don't use Model View Presenter the best way to use Instructions is place their listener inside your receiver component presenter.
The goal of this approch is that you could use the Instruction model inside your existent application without any component change.
We'll soon add an MVP sample that use instructions but you can now check the Model View Presenter post to get an MVP example
.
See also:
- ModelViewPresenter -- How to use the Model View Presenter design pattern to create your component
- Mapper
- MapperManager
THE MAIN MXML FILE
<?xml version="1.0" encoding="utf-8"?>
<mx:Application layout="horizontal"
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:receiver="views.receiver.view.*"
xmlns:sender="views.sender.view.*"
preinitialize="preinit(event)"
implements="com.gnstudio.nabiro.mvp.mapper.IMapperCandidate"
>
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
import com.gnstudio.nabiro.mvp.mapper.Mapper;
import com.gnstudio.nabiro.mvp.mapper.IMapperCandidate;
import core.DemoMapper;
private var mapper:Mapper;
/**
* Listener to the FlexEvent.PREINITIALIZE event
* it create the mapper and define Event.ADDED and Event.REMOVED listeners
*
* @param e FlexEvent
*/
private function preinit(e:FlexEvent):void{
// Create the mapper and set its target (i.e. SystemManager)
mapper = DemoMapper.getInstance(this);
// Define the listeners in order to add and remove elements from the mapper
this.addEventListener(Event.REMOVED, onItemRemoved);
this.addEventListener(Event.ADDED, onItemAdded);
}
/**
* Listener to the Event.REMOVED event
* it removes an element to the mapper if the element is marked for
* @param e Event
*/
private function onItemRemoved(e:Event):void{
if(e.target is IMapperCandidate){
mapper.unregister(e.target as IMapperCandidate);
}
}
/**
* Listener to the Event.ADDED event
* it adds an element to the mapper if the element is marked for
* @param e Event
*/
private function onItemAdded(e:Event):void{
if(e.target is IMapperCandidate){
mapper.register(e.target as IMapperCandidate);
}
}
]]>
</mx:Script>
<receiver:ReceiverView id="receiver" />
<sender:SenderView id="sender" />
</mx:Application>
The sender component
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300"
implements="com.gnstudio.nabiro.mvp.mapper.IMapperCandidate">
<mx:Script>
<![CDATA[
import instructions.DemoInstruction;
import events.DemoInstructionEvent;
private function sendInstruction():void {
// Send an Instruction
dispatchEvent(new DemoInstructionEvent("DemoInstruction"));
}
]]>
</mx:Script>
<mx:Button label="Send Instruction" click="sendInstruction()" />
</mx:VBox>
The receiver component
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300"
implements="com.gnstudio.nabiro.mvp.mapper.IMapperCandidate"
creationComplete="init()">
<mx:Script>
<![CDATA[
import instructions.DemoInstruction;
private function init():void {
// Listen for an Instruction (i.e. DemoInstruction)
addEventListener( DemoInstruction.DO_HANDLE, onDemoInstruction);
}
public function onDemoInstruction(e:DemoInstruction):void {
listenerText.text = "Instruction Arrived"
}
]]>
</mx:Script>
<mx:Label id="listenerText" text="listining...." />
</mx:VBox>
The DemoInstruction?
package instructions
{
import com.gnstudio.nabiro.mvp.instructions.Instruction;
import flash.events.IEventDispatcher;
import flash.events.Event;
public class DemoInstruction extends Instruction
{
public static const DO_HANDLE:String = "onDemoInstruction";
public function DemoInstruction(o:IEventDispatcher, params:*=null, bubbles:Boolean=true, cancelable:Boolean=false){
super(o, DO_HANDLE, params, bubbles, cancelable);
}
override public function clone():Event {
return new DemoInstruction(origin, parameters);
}
}
}
package events
{
import com.gnstudio.nabiro.mvp.instructions.InstructionEvent;
import flash.events.Event;
public class DemoInstructionEvent extends InstructionEvent
{
public function DemoInstructionEvent(instruction:String, params:*=null, preventable:Boolean = false, bubbles:Boolean=true, cancelable:Boolean=true){
super("instructions." + instruction, params, preventable, bubbles, cancelable);
}
public override function clone():Event {
return new DemoInstructionEvent(toPerform, parameters);
}
public override function toString():String {
return formatToString("DemoInstructionEvent", "type", "bubbles", "cancelable", "eventPhase", "toPerform", "parameters");
}
}
}
The DemoMapper?
package core{
import com.gnstudio.nabiro.mvp.instructions.InstructionEvent;
import com.gnstudio.nabiro.mvp.mapper.Mapper;
import flash.events.IEventDispatcher;
import instructions.DemoInstruction;
public class DemoMapper extends Mapper{
private static var _instance:DemoMapper = new DemoMapper ();
/**
* Compsition bugs fix.
*/
private var demoInstruction:DemoInstruction;
public function DemoMapper (){
if (_instance != null) throw new Error("SnippetMapper is obviously Singleton.");
}
public static function getInstance(target:IEventDispatcher = null):DemoMapper {
if(target){
_instance.registerTarget(target);
}
return _instance;
}
override protected function onInstruction(e:InstructionEvent):void{
try{
super.onInstruction(e);
}catch(error:Error){
trace("\t", "Keep", e.toPerform, "on the system manager")
}
if(e.isPreventable){
e.stopImmediatePropagation();
}
}
}
}
Attachments
-
2009_09_17_Instructions_SourceCode.zip
(1.2 MB) - added by flagers
6 months ago.
How to use Instruction and Mapper - Source Code (FX)