views:

30

answers:

1

I have created a custom event in flex 3.5. But the handler is not invoked. How to solve this or what is the way to debug this problem?

The Event class:

package com.saneef.worldlanguages.events
{
    import flash.events.Event;

    public class LanguageEvent extends Event
    {
        public static const LANGUAGE_SELECTED:String = "LanguageSelected";

        public function LanguageEvent(type:String,languageid:String)
        {
            super(type);
            this.langid = languageid;
            trace("LanguageEvent: " + this.langid);
        }

        public var langid:String;

        override public function clone():Event {
            return new LanguageEvent(type, langid);
        }
    }
}

Dispatching:

private function functionOne():void
{       
    try{
        dispatchEvent(new LanguageEvent(LanguageEvent.LANGUAGE_SELECTED,"STR"));
    }
    catch(e:Error)
    {
        trace(e.message);
    }
}

In the Main application class, EventListener:

protected function application1_initializeHandler(event:FlexEvent):void
{
  this.addEventListener(LanguageEvent.LANGUAGE_SELECTED,
 application1_LanguageSelectionHandler);
}

The event handler function:

public function application1_LanguageSelectionHandler(event:LanguageEvent):void
{
    trace("application1_LanguageSelectionHandler: " + event.langid);
    populate_countrya3id_languages(event.langid);
}
+3  A: 

Your code looks fine. Since I can't see the full source, here are my two thoughts on what may be going on:

  1. Are you sure your addEventListener call is done before you dispatch the event? Add some trace to make sure the application1_initializeHandler prints before functionOne does.

  2. Is your functionOne call in another different component than your main application? If so, you'll need to set your custom event's bubbles attribute to true in your event's super call.

     public function LanguageEvent(type:String,languageid:String,bubbles:Boolean=True)
     {
        super(type, bubbles);
        this.langid = languageid;
        trace("LanguageEvent: " + this.langid);
     }
    

See the flash.events.Event docs for the constructor call. Also, here's a quote about the bubbles argument explained here:

The bubbles property

An event is said to bubble if its event object participates in the bubbling phase of the event flow, which means that the event object is passed from the target node back through its ancestors until it reaches the Stage. The Event.bubbles property stores a Boolean value that indicates whether the event object participates in the bubbling phase. Because all events that bubble also participate in the capture and target phases, any event that bubbles participates in all three of the event flow phases. If the value is true, the event object participates in all three phases. If the value is false, the event object does not participate in the bubbling phase.

Based on your source code, it looks like you've seen the "Dispatching Custom Events" in the flex docs, but I'll link to it anyways for future/easy reference: http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html.

Also, check out http://www.adnandoric.com/2008/12/29/understanding-the-flex-event-propagation/ for a high-level overview of the event propagation system to try to get a better understanding of what's going on while developing.

Edit:

Based on your comments I'm guessing your functionOne call is in a separate class and your "main" application has an instance of this class. If that's so you'll want to attach your event listener on that instance and delegate it to your main's application1_LanguageSelectionHandler function... Like so:

protected function application1_initializeHandler(event:FlexEvent):void
{
  this.theInstanceThatHoldsYourFunctionOne.addEventListener(LanguageEvent.LANGUAGE_SELECTED,
 application1_LanguageSelectionHandler);
}
sdolan
"addEventListener" for the event is executed before the dispatch. I have tested with trace. As you have doubted "functionOne" is in another Class other than the main application. I have added the 'bubbles = true' in super class constructor as you have suggested. But still it is not working. I'll go through the links you have provided and try debugging more. :)
Saneef
@Saneef: Based on your comment I've updated my answer... check it out and see if that solves it for you.
sdolan
@sdolan It worked. :) Thank you! Now, I'm having doubt, even if I add the event listener to the main application (like i did earlier), won't that be invoked while the event propagation like the one showed in the adnandoric.com?
Saneef
@Saneef: Glad that worked out for you. Could you try to clarify what you mean by "won't that be invoked while the event propagation"?
sdolan
I meant: if I use "this.addEventListener(LanguageEvent.LANGUAGE_SELECTED, application1_LanguageSelectionHandler);" in the Main application, why it is not working, since it is said that the event propagation happens like explained in the link.
Saneef
The event propgation that's explained in the link described `DisplayObject` propagation (`HBox`, `VBox`, etc.). Since your `theInstanceThatHoldsYourFunctionOne` instance is not a part of the display hierarchy, you have to attach it programatically.
sdolan