Neither the object that dispatches the event nor the
object that listens to that need to be in a display
list.
I am attaching herewith the code files that I wrote to
test the same thing that you are asking. The event
dispatcher is not the display object in my sample. The
listeners are the functions of the main application
object, however, you can most those functions into any
other non-display object. Below is the description of
code files.
1. CustomEventDispathcer.as - My custom implementation
of event dispatcher that internally use instance
EventDispatcher.
2. CustomEvent - Extends event class to introducse
custom properties.
3. EventTest.mxml - Main application MXML
I have also deisgned a publisher-subscriber scenario
using the almost same technique of the test classes.
But I can't share it as it's my company proprietory.
Hope this helps.
--- bithroop <[EMAIL PROTECTED]> wrote:
> Thanks for the responses so far. I need to clarify
> what I'm asking I
> think.
>
> I realized after I wrote my post that since I had
> extended mx:object
> with MXML that I wouldn't be able to dispatch events
> from it. That
> really wasn't my issue though. Let's back up and say
> that I had
> extended EventDispatcher, or for that matter,
> something that _could_
> be a display object but isn't in the display list,
> like mx:Canvas.
>
> What I'm really wondering is who can hear events
> dispatched from
> instances that aren't in the display list (have been
> instantiated but
> not addChild'd). Can any object at all? Can I direct
> who can hear
> them? My impression is that if an instance is not in
> a display list
> then it is effectively deaf and mute as far as
> events are concerned.
> This seems wrong, but I don't know how else it
> should work.
>
> --- In [email protected], Jehanzeb Musani
> <[EMAIL PROTECTED]>
> wrote:
> >
> > You can adopt the following two options to achieve
> > your requirements.
> >
> > 1. Your MXML Object should Implement the
> > IEventDispatcher interface and internally it
> creates
> > an instance of EventDispathcer class and delegates
> > calls to that instance.
> >
> > 2. If you dont want to use built-in event
> dispatcher
> > and you want to define your own function
> definition,
> > then you can defines an interface and all the
> > listeners should implement that interface.
> Moreover,
> > you should methods in your MXML Object to register
> and
> > unregister those listeners and call the specific
> > method of that interface on some condition.
> >
> > Gordon, I want to ask you a few questions
> regarding
> > event dispatching mechanism of default
> > EventDispatcher.
> >
> > 1. Does EventDispatcher calls the listeners
> > synchronously or asynchronously?
> >
> > 2. Does the EventDispatcher make copies of the
> event
> > object provided to EventDispatcher's dispatchEvent
> > function when calling the listeners? Let say, I
> define
> > 2 listeners for specific event type. The first
> > listener modify some properties of the event
> object.
> > Do the second listener get the modified one or the
> > copy of the original one?
> >
> > --- Gordon Smith <[EMAIL PROTECTED]> wrote:
> >
> > > Or your Object subclass could create an
> > > EventDispatcher instance which
> > > can dispatch events on its behalf:
> > >
> > > var eventDispatcher:EventDispatcher = new
> > > EventDispatcher(this);
> > > eventDispatcher.dispatchEvent(event);
> > >
> > > Gordon Smith
> > > Adobe Flex SDK Team
> > >
> > > ________________________________
> > >
> > > From: [email protected]
> > > [mailto:[EMAIL PROTECTED] On
> > > Behalf Of Alex Harui
> > > Sent: Wednesday, November 28, 2007 1:26 PM
> > > To: [email protected]
> > > Subject: RE: [flexcoders] Events and Non-Display
> > > Objects
> > >
> > >
> > >
> > > You can try extending EventDispatcher and
> dispatch
> > > events
> > >
> > > ________________________________
> > >
> > > From: [email protected]
> > > [mailto:[EMAIL PROTECTED] On
> > > Behalf Of bithroop
> > > Sent: Wednesday, November 28, 2007 10:18 AM
> > > To: [email protected]
> > > Subject: [flexcoders] Events and Non-Display
> Objects
> > >
> > >
> > >
> > > General question here...
> > >
> > > I understand Event flow with objects in the
> display
> > > list. They flow
> > > either up (bubbling) or down (capture) between
> > > parent and child. Maybe
> > > an oversimplification but my question really is
> > > about how to handle
> > > events with objects that are not in the display
> > > list. How do you pass
> > > events from them? Like for instance, I've got
> this
> > > situation here...
> > >
> > > I have an MXML that is just an <mx:Object>. It's
> > > MXML because I'm
> > > defining some remoteObjects in it and that's
> just
> > > way more fun to do
> > > in MXML than in AS. So anyway, regardless, the
> > > methods inside are
> > > static and I don't want to draw the thing. It
> just
> > > needs to hang out
> > > and be available so I can make these remoting
> calls.
> > >
> > > So there's the rub. How can this MXML talk back
> to
> > > where it was called
> > > from? Can I use events for this or do I need to
> send
> > > a callback
> > > function?
> > >
> > > Any wisdom here would be awesome... this has
> been
> > > befuddling me for a
> > > while.
> > >
> > > -b
> > >
> > >
> > >
> > >
> > >
> >
> >
> >
> >
>
____________________________________________________________________________________
> > Get easy, one-click access to your favorites.
> > Make Yahoo! your homepage.
> > http://www.yahoo.com/r/hs
> >
>
>
>
____________________________________________________________________________________
Get easy, one-click access to your favorites.
Make Yahoo! your homepage.
http://www.yahoo.com/r/hs 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>