10 Replies Latest reply: Sep 3, 2010 9:54 AM by Flex harUI RSS

    Flex events: when to use capture phase

    ilya_k Community Member

      Greetings!

       

      I seem to understand Event flow (maybe I do not) but  have difficulties to figure out 

      why should I use capture phase event in adddEventListener? Anyone can think of use case?

      Why would I create custom event with capture not bubbling as default?

       

      Regards

        • 1. Re: Flex events: when to use capture phase
          Flex harUI Adobe Employee

          Bubbling events is generally a bad practice.  Bubbling should be reserved

          for events that can be dispatched my more than one children of varying types

          within a container.  That's why only things like adding/removing children,

          mouse and keyboard activity and focus bubble.

           

          Capture is used by a container to react to an event and possibly prevent the

          actual target's listeners from seeing it.  It's primary use is in handling

          mouse dragging, similar to the MouseCapture() API in Windows.

          • 2. Re: Flex events: when to use capture phase
            supersonicecho Community Member

            Alex, why is bubbling events considered bad practice?  I am really curious because I make heavy use of bubbling to send information to the systemManager to act as a controller.

             

            Also seems useful to me as a way to more loosely couple components.  But, I am not trying to be combative or defensive, just explaining why I use bubbling.

            • 3. Re: Flex events: when to use capture phase
              Flex harUI Adobe Employee

              Because every level of the display tree gets to see the event, and it

              requires that you come up with unique event names.  It essentially breaks

              encapsulation and takes longer to process.

               

              You might want to look at how other application frameworks set up their

              controllers.

              • 4. Re: Flex events: when to use capture phase
                supersonicecho Community Member

                Thanks for the explanation.

                • 5. Re: Flex events: when to use capture phase
                  ilya_k Community Member

                  Thank you Alex for explanation. 

                  One more question though.

                   

                  What will happen if  in AS class somewhere I have added the listener as follows:

                  addEventListener( FlexEvent.APPLICATION_COMPLETE,  <listener>, false )

                  but FlexEvent defined with bubbles=false? Will I miss an event?

                   

                  Regards

                  • 6. Re: Flex events: when to use capture phase
                    Flex harUI Adobe Employee

                    APPLICATION_COMPLETE is only dispatched from the Application and

                    SystemManager.  If you are not listening there you won't see it.

                     

                    Note that, if you had a sub-app that bubbled APPLICATION_COMPLETE, the

                    main-app would see it and potentially run its APPLICATION_COMPLETE handlers

                    again, which is probably not desirable.

                    • 7. Re: Flex events: when to use capture phase
                      ilya_k Community Member

                      It is exactly my case.

                      I have loader class and sup-application(s) it loads. Loader needs to know when sub-app

                      finishes its initialization (APPLICATION_COMPLETE event happened) in order to invoke

                      sub-app.methods. So,  loader in Event.COMPLETE handler has:

                       

                             public function onSubappLoaded( event:Event ):void

                              {

                                  var swfLoader:SWFLoader = event.target as SWFLoader;

                                  swfLoader.removeEventListener( Event.COMPLETE, onSubappLoaded );

                                  swfLoader.removeEventListener( IOErrorEvent.IO_ERROR, onSubappLoadError );

                       

                                  loadedApp = event.target.content as ISystemManager;

                                  if ( loadedApp != null )

                                  {

                                      loadedApp.addEventListener( FlexEvent.APPLICATION_COMPLETE, onSubappConstructed, false, 3 );

                                  }

                            }

                       

                              public function onSubappConstructed( event:FlexEvent ):void

                              {

                                  loadedApp.removeEventListener( FlexEvent.APPLICATION_COMPLETE, onSubappConstructed );

                                  try

                                  {

                                      // cast the loaded application to the interface it implements

                                      var loadable:ISCCLoadable = event.currentTarget.application as ISCCLoadable;

                                      sccLoadable = loadable as ISCCLoadable;

                                  }

                                  catch (e:Error)

                                  {

                                          ...

                                 }

                      So, my question was, is it correct to add listener as above or it is better to change
                      to ddEventListener( FlexEvent.APPLICATION_COMPLETE, onAppConstructed, true );
                      Thank you very much

                      • 8. Re: Flex events: when to use capture phase
                        Flex harUI Adobe Employee

                        There is no capture phase at the target.  It makes more sense to listen to

                        the sub-app than one of its parents.

                        • 9. Re: Flex events: when to use capture phase
                          ilya_k Community Member

                          Sorry, Alex, It was typo in my previous post. What  I meant to ask was:

                           

                          In sub-app loader should I listen APPLICATION_COMPLETE event

                           

                          like this:

                          addEventListener( FlexEvent.APPLICATION_COMPLETE, onSubappConstructed, true );

                           

                          or like this:

                          addEventListener( FlexEvent.APPLICATION_COMPLETE, onSubappConstructed, false );

                           

                          or it does not matter?

                           

                          Thank you.

                          • 10. Re: Flex events: when to use capture phase
                            Flex harUI Adobe Employee

                            It matters, as there is no capture phase at the target.  If you are

                            listening to the systemManager, you must use false.  Using true will work in

                            this case because the child application also dispatches the event, but it

                            would not work in the general case