OK, here's a little app to demonstrate the problem. I'm sure the
answer is obvious, but... can someone point it out?

The Application and the Canvas go in the src folder; the custom event
(FoodEvent) goes in an 'events' folder inside src.

I made the canvas because I wanted the event to be heard in a
different component from the Application itself.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; 
layout="horizontal" xmlns:food = "*">
        <mx:Script>
                <![CDATA[
                
                import events.FoodEvent;
                
          [Bindable]
          public var food_supply:Array = [{label:"Pizza Pie",
data:"Pizza"},{label:"Buffalo Wings", data:"Wings"}]; 
          
        private function onFoodCBChange():void
        {
                var item:Object = foodCB.selectedItem;
                if(item == null)
                        return;

                var event:FoodEvent = new FoodEvent(FoodEvent.FOOD_EVENT);
                event.selectedItemFromFoodCB = item.data;
                this.dispatchEvent(event);
        }
                ]]>
        </mx:Script>
                        <mx:Canvas height="167" width="200">
                                  <mx:ComboBox x="40" y="20" 
dataProvider="{food_supply}"
id="foodCB"  textAlign="left" change="onFoodCBChange()"/>
                         </mx:Canvas>
                        <food:testCanvas />
</mx:Application>

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml";
creationComplete="this.addEventListener(FoodEvent.FOOD_EVENT,
foodEventhandler)"
         borderStyle="solid" borderColor="red" borderThickness="3" x="300"
y="300" width="200" height="200">
        <mx:Script>
                <![CDATA[
                        import events.FoodEvent;
                        import mx.controls.Alert;
                        
                        private function foodEventhandler(e:FoodEvent):void
                        {
                                Alert.show(e.selectedItemFromFoodCB);
                        }
                
                ]]>
        </mx:Script>
</mx:Canvas>

package events
{
        import flash.events.Event;

        public class FoodEvent extends Event
        {

                public static const FOOD_EVENT:String   = 'food.event';
                
                public var selectedItemFromFoodCB:String;       //Used to pass 
selection value
                
                public function FoodEvent(type:String, bubbles:Boolean=false,
cancelable:Boolean=false)
                {
                        super(type, bubbles, cancelable);
                }
                
        }
}

--- In [email protected], "Tracy Spratt" <tspr...@...> wrote:
>
> It will depend on where and how the listener is declared.
> CreationComplete fires at different times for different components.  As
> I recall, it is bottom up, so creationComplete for a parent will fire
> after creationComplete for a child.  If the parent creationComplete
> handler declares the listener, then there would be a problem.
> 
>  
> 
> This should only be an issue on start-up.  Certainly the listener would
> be available when the app was ready to receive user input, via ComboBox
> or button click, as the original poster noticed.
> 
>  
> 
> Remember that events flow through the DOM.  It is possible for a
> dispatched event to "miss" a component.  Pop-ups are a particular
> problem.
> 
>  
> 
> Tracy
> 
>  
> 
> ________________________________
> 
> From: [email protected] [mailto:[email protected]] On
> Behalf Of stldvd
> Sent: Monday, January 05, 2009 4:48 PM
> To: [email protected]
> Subject: [flexcoders] Re: why I can not dispatchEvent with
> creationComplete?
> 
>  
> 
> I'm having a similar issue. Event is dispatched (I can step through
> code and see it) but the event listener is never hearing it, and so
> the listener function is never being fired.
> 
> How does one tell if the listener is ready or not? If you add the
> listener on the creationcomplete or initialize event of an mxml
> component, it should in theory be ready to fire, for the duration of
> the app, right? And if you're dispatching a custom event when an item
> is selected in a combobox, it should certainly work. What could be the
> reason for a 'deaf' event listener? :)
> 
> -- David
> 
> --- In [email protected] <mailto:flexcoders%40yahoogroups.com>
> , "Tracy Spratt" <tspratt@> wrote:
> >
> > Perhaps the listener is not yet ready when the canvas creationComplete
> > fires?
> > 
> > Tracy
> > 
> > 
> > 
> > ________________________________
> > 
> > From: [email protected] <mailto:flexcoders%40yahoogroups.com>
> [mailto:[email protected] <mailto:flexcoders%40yahoogroups.com>
> ] On
> > Behalf Of markflex2007
> > Sent: Monday, January 05, 2009 2:08 PM
> > To: [email protected] <mailto:flexcoders%40yahoogroups.com> 
> > Subject: [flexcoders] why I can not dispatchEvent with
> creationComplete?
> > 
> > 
> > 
> > I try the following code, But I can not dispatchEvent.
> > 
> > <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml
> <http://www.adobe.com/2006/mxml> 
> > <http://www.adobe.com/2006/mxml <http://www.adobe.com/2006/mxml> > "
> > creationComplete="justLogin()" >
> > <mx:Metadata>
> > [Event(name="loginEvent", type="flash.events.Event")]
> > </mx:Metadata>
> > <mx:Script>
> > <![CDATA[
> > 
> > 
> > private function justLogin():void{ 
> > 
> > dispatchEvent(new Event("loginEvent", false));
> > 
> > }
> > ]]>
> > </mx:Script> 
> > 
> > </mx:Canvas>
> > 
> > I can dispatchEvent with button click, please let me know how to
> > automatic dispatchEvent in page start.
> > 
> > Thanks
> > 
> > Mark
> >
>


Reply via email to