开发者

Flex event handler not working

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);
}


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜