Aaron H.
Aaron H.

Reputation: 6597

Flex: Why can't I handle certain events?

In certain cases, I can't seem to get components to receive events.

[edit]

To clarify, the example code is just for demonstration sake, what I was really asking was if there was a central location that a listener could be added, to which one can reliably dispatch events to and from arbitrary objects.

I ended up using parentApplication to dispatch and receive the event I needed to handle.

[/edit]

If two components have differing parents, or as in the example below, one is a popup, it would seem the event never reaches the listener (See the method "popUp" for the dispatch that doesn't work):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
        layout="absolute" 
        initialize="init()">
<mx:Script>
    <![CDATA[
        import mx.controls.Menu;
        import mx.managers.PopUpManager;

        // Add listeners
        public function init():void
        {
            this.addEventListener("child", handleChild);
            this.addEventListener("stepchild", handleStepchild);
        }

        // Handle the no pop button event
        public function noPop(event:Event):void
        {
            dispatchEvent(new Event("child"));
        }

        // Handle the pop up
        public function popUp(event:Event):void
        {
            var menu:Menu = new Menu();
            var btnMenu:Button = new Button();
            btnMenu.label = "stepchild";
            menu.addChild(btnMenu);
            PopUpManager.addPopUp(menu, this);

            // neither of these work...
            this.callLater(btnMenu.dispatchEvent, [new Event("stepchild", true)]);
            btnMenu.dispatchEvent(new Event("stepchild", true));
        }

        // Event handlers

        public function handleChild(event:Event):void 
        {
            trace("I handled child");
        }

        public function handleStepchild(event:Event):void {
            trace("I handled stepchild");
        }
    ]]>
</mx:Script>

<mx:VBox>
    <mx:Button label="NoPop" id="btnNoPop" click="noPop(event)"/>
    <mx:Button label="PopUp" id="btnPop" click="popUp(event)"/>
</mx:VBox>
</mx:Application>

I can think of work-arounds, but it seems like there ought to be some central event bus...

Am I missing something?

Upvotes: 2

Views: 1139

Answers (2)

Verdant
Verdant

Reputation: 393

Above is correct. You are dispatching the event from btnMenu, but you are not listening for events on btnMenu - you are listening for events on the Application.

Either dispatch from Application:

dispatchEvent(new Event("stepchild", true));

or listen on the btnMenu

btnMenu.addEventListener("stepchild",handleStepChild);
btnMenu.dispatchEvent(new Event("stepchild",true));

Upvotes: 2

Antti
Antti

Reputation: 3159

You are attaching the listener to this when the event is getting dispatched from btnMenu.

This should work:

dispatchEvent(new Event("stepchild", true));

ps. There is really no reason to put an unnecessary 'this' everywhere, unless it's explicitly required to overcome scope issues. In this case you can just leave every this out.

Upvotes: 0

Related Questions