7 Replies Latest reply on Aug 26, 2010 10:34 AM by GordonSmith

    Event propagation

    Farore

      Good day everybody,

       

      I got a question about event propagation.

       

      My situation is as following; I got an Application.mxml with 2 child components, let's say Comp1.mxml and Comp2.mxml.

      I dispatch a custom event in Comp2.mxml which I want to handle in Comp1.mxml.

       

      How can I directly listen for an event from Comp2 in Comp1 without having to bubble it to the root application first?

      I thought the useCapture could help me with that but I can't seem to get it to work.

       

      What I have now is the following:

       

      Comp2.mxml -> dispatchEvent with bubbles set to true

      |

      Application.mxml -> addEventListener(event, comp1.eventHandler)

      |

      Comp1.mxml -> public function eventHandler() {}

       

      The above works just fine, but I don't like the fact I listen for the event in Application while Comp1 is the only component that needs to deal with it.

       

      Is there any possibility to just listen for the event from Comp2 directly in Comp1?

       

      Thanks in advance!

        • 1. Re: Event propagation
          BhaskerChari Level 4

          Hi Farore,

           

          You can register and dispatch events w.r.t systemManager when you want to listen events in components/Objects which do not participate in

           

          event propagation...

           

          So dispatch event in your comp2 as systemManager.dispatchEvent(YourEvent);  and in your Comp1 listen it w.r.t systemManager again..

           

          systemManager.addEventListener(YourEvent, eventHandler);

           

          This should just work fine for you..

           

           

          Thanks,

          Bhasker

          • 2. Re: Event propagation
            Farore Level 1

            Hi BhaskerChari,


            Thanks for your quick reply. It works indeed.

            • 3. Re: Event propagation
              GordonSmith Level 4

              > Is there any possibility to just listen for the event from Comp2 directly in Comp1?

               

              Sure. You don't have to use bubbling and SystemManager. Comp1 can just register for the event from Comp2 with code like

               

              instanceOfComp2.addEventListener("someEvent", handlerInComp1)

               

              Gordon Smith

              Adobe Flex SDK Team

              • 4. Re: Event propagation
                Farore Level 1

                Hi Gordon,

                 

                Thank for your reply.

                 

                I realised I could just use parentApplication.comp2.addEventListener in Comp1.

                To make things short I now use the parentApplication to dispatch events and listen to them.

                 

                comp1

                - parentApplication.dispatchEvent(new CustomEvent(CustomEvent.MY_EVENT));

                 

                comp2

                - parentapplication.addEventListener(CustomEvent.MY_EVENT, myEventHandler);

                • 5. Re: Event propagation
                  BhaskerChari Level 4

                  Hi Farore,

                   

                  I think this styl of event handling only gonna work if your both components are in the main mxml file only...What say if your comp1 is in main mxml

                   

                  and comp2 is main mxml and say there is another component comp3 in the comp1 itself then if you are dispatching the event in the comp2 then how you

                   

                  gonna listen to that event in comp3....???

                   

                   

                   

                  Thanks,

                  Bhasker

                  • 6. Re: Event propagation
                    Farore Level 1

                    Hi Bhasker,

                     

                    I think I can just accomplish that by doing the exact same in comp3.

                     

                    parentApplication.addEventListener(CustomEvent.MY_EVENT, handleMyEvent);

                     

                    since it is dispatched like parentApplication.dispatchEvent(...) in comp2.

                    It is dispatched for parentApplication, which means I could just listen for it in another nested child or isn't that gonna work?

                    • 7. Re: Event propagation
                      GordonSmith Level 4

                      I wouldn't recommend using the application as a surrogate event dispatcher for a component. In general, components should dispatch their own events as part of proper object-oriented component design (unless you're using some application framework with centralized event dispatching). Any object can listen to any IEventDispatcher, as long as it has a reference to it. If you don't like having to do

                       

                      parentApplication.comp2.addEventListener(...)

                       

                      in Comp1 then I recommend passing a reference-to-Comp2 to Comp1 when Comp1 is constructed. Just consider it a property of Comp1 which tells Comp1 who to listen to.

                       

                      Gordon Smith

                      Adobe Flex SDK Team