Solved

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

Posted on 2010-09-13
7
853 Views
Last Modified: 2013-11-11
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

0
Comment
Question by:DReade83
  • 4
  • 3
7 Comments
 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 500 total points
ID: 33665978
Hi,

glad you picked up my suggestion regarding the Events. Let me explain what's going on:
As soon as the main component is initialized, the popup is opened and the popup is a direct child of the main application.
Parallel to the Dialog the main application has another child, your component.

In your dialog you now fire an event in your dialog. So flex checks if the dialog itself has any listeners. Unfortunately not. This is why your component will never get the event.

What you would need, is to catch the event in the main application and to throw it at the component model directly.

For further reading I would suggest having a look at the event processing phases (Targeting Phase, Bubbling Phase, ...) to get a better understanding at how flex handles this.

Hope my modified code works for you.
// TestApp.mxml (application)

<?xml version="1.0" encoding="utf-8"?>

<s:Application 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.controls.Alert;

			import mx.events.DynamicEvent;

			import mx.managers.PopUpManager;

			

			protected function initApp():void

			{

				var popUp:MyPopUp = new MyPopUp();

				popUp.addEventListener("myEvent", myEventListenerDirect);

				PopUpManager.addPopUp(popUp, this);

			}

			

			protected function myEventListenerDirect(event:DynamicEvent):void {

				myComp.dispatchEvent(event);

			}

		]]>

	</fx:Script>

	<com:MyComp id="myComp"/>

</s:Application>





    // 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:Metadata>

		[Event(name="myEvent", type="mx.events.DynamicEvent")]

	</fx:Metadata>

	

	<fx:Script>

		<![CDATA[

			import mx.controls.Alert;

			import mx.events.DynamicEvent;

			import mx.managers.PopUpManager;

			

			private function call(event:MouseEvent):void

			{

				Alert.show('call()');



				var nonBubblingEvt:DynamicEvent = new DynamicEvent("myEvent");

				nonBubblingEvt.value1 = "1234";

				dispatchEvent(nonBubblingEvt);



				PopUpManager.removePopUp(this);

			}

		]]>

	</fx:Script>

	

	<s:Button click="call(event)" label="Call Event"/>

</s:Group>

Open in new window

0
 

Author Comment

by:DReade83
ID: 33670243
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?
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33670448
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:DReade83
ID: 33670464
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. :-)
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33672444
So how about closing this question? I like to have my "open question" list nice and clean ;-)
0
 

Author Comment

by:DReade83
ID: 33672740
Oops... There you go. :-)
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33672797
Thanks ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I convert a CS4 FLA file to CS6? 1 864
Adobe Flash Player : screen freezes for a few GUI & Rdp 5 609
Online Course Not Responding 3 89
EMET 5.2 / Adobe Flash 2 229
Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now