Link to home
Start Free TrialLog in
Avatar of DReade83
DReade83Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Flex 4: Event Listener created, but not being called?

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 and examples 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>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ChristoferDutz
ChristoferDutz
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DReade83

ASKER

Thanks very much.

On another site, someone suggested using Spring Framework and the EventBus to accomplish this. I've given it a go and it does indeed work.

Would you say this is a global event handler/listener? Do you think there would be any problems using this method?
Whooow ... Well you could certainly do this, but this would be as if you Flew your grandma to the supermarket using a brand-new Airbus A380 ;-) ... and it's certainly not faster.

You are trying to do a very simple task - I would suggest you stick to the simple solution. But if it works ... I just thought you wanted to learn Flex and I would suggest you dig into the flex event systen or you will have a really hard time understanding how you have to build your applications.
Yeah I totally understand what you mean. I'd like to avoid using frameworks if I can, as I've done in the past with PHP. I'll look into it some more. Thanks for your help though, much appreciated. :-)
So how about closing this question? I like to have my "open question" list nice and clean ;-)
Oops... There you go. :-)
Thanks ;-)