Jehanzeb - first, thank you for the detailed response! I really
appreciate it.
In the code you provided, the CustomEventDispatcher was instantiated
within the mx:Application object. So from what I've seen, yes
Application will be able to hear CustomEventDispatcher's events.
However, my sitation is more like:
Application
|
|------>ContainerC->ContainerD
|
|------>ContainerA->Custom AS class not in display
list->CustomEventDispatcher.
And I want CustomEventDispatcher's event to be heard by Container D.
Right now I have my listener on Application, and then Application is
calling a method on ContainerC, which is then calling a method on
ContainerD. This seems to be ugly and bad form.
So instead, will this work? (see below pseudocode)
---------------------------
inside ContainerD
addEventListener("testEvent", someHandler, true) //is capture needed?
inside CustomEventDispatcher
dispatchEvent(new Event("testEvent")) //is bubbling needed?
I have a feeling there's something really simple I'm not getting.
-b
> package myComponents
> {
> import flash.events.EventDispatcher;
> import flash.events.Event;
> import myComponents.events.CustomEvent;
>
> public class CustomEventDispatcher
> {
> public static const PRECHANGING:String = "PreChanging";
> public static const CHANGED:String = "Changed";
>
> private var itemName:String = null;
> private var eventDispatcher:EventDispatcher = new
> EventDispatcher();
>
> public function get ItemName() : String
> {
> return this.itemName;
> }
>
> public function set ItemName(value:String) : void
> {
> //this.eventDispatcher.dispatchEvent();
> this.eventDispatcher.dispatchEvent(new
CustomEvent(CustomEventDispatcher.PRECHANGING, false, false,
this.itemName));
> this.itemName = value;
> this.eventDispatcher.dispatchEvent(new
CustomEvent(CustomEventDispatcher.CHANGED, false, false, this.itemName));
> }
>
> public function addEventListener(type:String,
> listener:Function) :
void
> {
> this.eventDispatcher.addEventListener(type, listener);
> }
>
> public function reomveEventListener(type:String,
listener:Function) : void
> {
> this.eventDispatcher.removeEventListener(type,
> listener);
> }
> }
> }
> package myComponents.events
> {
> import flash.events.Event;
>
> public class CustomEvent extends Event
> {
> private var itemName:String;
>
> /*
> public function CustomEvent(type:String, bubble:Boolean,
cancelabl:Boolean)
> {
> this(type, bubble, cancelable, null);
> }
> */
>
> public function CustomEvent(type:String, bubble:Boolean,
cancelabl:Boolean, itemName:String)
> {
> super(type, bubble, cancelable);
> this.itemName = itemName;
> }
>
> public function get ItemName() : String
> {
> return this.itemName;
> }
> }
> }
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
> creationComplete="this.OnAppCreationComplete(event);" >
> <mx:Script>
> <![CDATA[
> import mx.utils.StringUtil;
> import mx.controls.Alert;
> import myComponents.events.CustomEvent;
> import myComponents.CustomEventDispatcher;
>
> private var customEventDispatcher:CustomEventDispatcher;
>
> private function OnAppCreationComplete(event:Event) :
> void
> {
> this.customEventDispatcher = new
> CustomEventDispatcher();
> this.customEventDispatcher.ItemName = "Hello
> World!";
>
>
this.customEventDispatcher.addEventListener(CustomEventDispatcher.PRECHANGING,
this.OnPreChanging);
>
this.customEventDispatcher.addEventListener(CustomEventDispatcher.CHANGED,
this.OnChanged);
> }
>
> private function OnPreChanging1(event:CustomEvent) :
> void
> {
> var message:String = "Event Type [0]. ItemName:
> [1].";
> message = StringUtil.substitute(message,
> event.type,
event.ItemName);
>
> mx.controls.Alert.show("Event Type: " +
> event.type + ".
ItemName: " + event.ItemName + ".", "PreChanging Alert");
> }
>
> private function OnPreChanging(event:CustomEvent) : void
> {
> var message:String = "Event Type [0]. ItemName:
> [1].";
> message = StringUtil.substitute(message,
> event.type,
event.ItemName);
>
> mx.controls.Alert.show("Event Type: " +
> event.type + ".
ItemName: " + event.ItemName + ".", "PreChanging Alert");
> }
>
> private function OnChanged(event:CustomEvent) : void
> {
> var message:String = "Event Type [0]. ItemName:
> [1].";
> message = StringUtil.substitute(message,
> event.type,
event.ItemName);
>
> mx.controls.Alert.show("Event Type: " +
> event.type + ".
ItemName: " + event.ItemName + ".", "Changed Alert");
> }
>
> private function OnBtnSetTextClick(event:MouseEvent) :
> void
> {
> this.customEventDispatcher.ItemName =
> this.txtItemName.text;
> }
> ]]>
> </mx:Script>
>
> <mx:VBox width="100%" height="100%">
> <!--
> <mx:FormItem id="frmItemName" label="Set Item Name"
> fontWeight="bold">
> <mx:Text id="txtItemName" text="" />
> </mx:FormItem>
> -->
> <mx:HBox>
> <mx:Label text="Item Name" fontWeight="bold"/>
> <mx:TextArea id="txtItemName" text=""/>
> </mx:HBox>
> <mx:Button id="btnSetText" label="Set Item Name"
click="this.OnBtnSetTextClick(event);" />
> </mx:VBox>
> </mx:Application>
>