views:

115

answers:

1

I'm trying to call an event I've created in another component. I've added trace() into my methods so I can see what's being called. Everything except for the event listener (myEvent) is being called. Can anyone tell me why this is please?

Any help would be greatly appreciated. Thanks in advance.

// TestApp.mxml (application)
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       xmlns:com="com.*"
                       creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
            import com.MyPopUp;

            import mx.managers.PopUpManager;
            protected function initApp():void
            {
                var popUp:MyPopUp = new MyPopUp();    

                PopUpManager.addPopUp(popUp, this);
            }    
        ]]>
    </fx:Script>
    <com:MyComp/>
</s:WindowedApplication>

// MyComp.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx"
          width="100%" height="100%"
          creationComplete="initComp()">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.DynamicEvent;

            protected function initComp():void
            {
                trace('init MyComp()');

                this.addEventListener('myEvent', myEvent);
            }

            private function myEvent(event:DynamicEvent):void
            {
                trace('myEvent()');

                Alert.show('Event Called!', 'Success');
            }
        ]]>
    </fx:Script>
</s:VGroup>

// MyPopUp.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" width="100" height="100">
    <fx:Script>
        <![CDATA[
            import mx.events.DynamicEvent;
            import mx.managers.PopUpManager;

            private function call(event:MouseEvent):void
            {
                trace('call()');

                PopUpManager.removePopUp(this);

                var evt:DynamicEvent = new DynamicEvent('myEvent');
                evt.value1 = '1234';

                dispatchEvent(evt);
            }
        ]]>
    </fx:Script>
    <s:Button click="call(event)" label="Call Event"/>
</s:Group>
+2  A: 

MyComp and MyPopup aren't in the same display list hierarchy, so the bubbling event isn't being seen.

If you are wanting to send messages across components in this way consider using some sort of global event dispatcher, using using a shared model between the two components in order to see data changes.

Gregor Kiddie
I'm new to Flex so could you provide some sort of example of what you mean please? Are there any Frameworks out there that can do this easily?
Reado
Adding event listeners to FlexGlobals.application(I'm using 3.5 so I've forgotten the correct property name) or creating a singleton implementation of I/EventDispatcher are two less elegant ways but quick solutions for a small application.
jooks
The Event Bus in Spring Actionscript is a good start.
Gregor Kiddie
Spring works spot on. Using a combination of EventBus.addEventListener() and EventBus.dispatchEvent() works great! Thanks very much! +1 :)
Reado