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.
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.
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
Thanks for the explanation.
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?
1 person found this helpful
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.
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 );
// cast the loaded application to the interface it implements
var loadable:ISCCLoadable = event.currentTarget.application as ISCCLoadable;
sccLoadable = loadable as ISCCLoadable;
}So, my question was, is it correct to add listener as above or it is better to changeto ddEventListener( FlexEvent.APPLICATION_COMPLETE, onAppConstructed, true );Thank you very much
There is no capture phase at the target. It makes more sense to listen to
the sub-app than one of its parents.
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
addEventListener( FlexEvent.APPLICATION_COMPLETE, onSubappConstructed, true );
or like this:
addEventListener( FlexEvent.APPLICATION_COMPLETE, onSubappConstructed, false );
or it does not matter?
1 person found this helpful
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