FLEX/AIR Dispatch Custom Events from Popup
Here's some kind of tutorial, just because I just needed it but couldn't find a howto for what I needed.
The problem was that I wasn't able to dispatch a custom event from a Popup and catch it in the main-mxml. Here we go:
Communicating with a PopUp is a common task. This article explores how to do that.
Here, a TitleWindow is used to present a choice of option. A button opens the Popup where the user can choose his option, or do other stuff ...
This is the code for the pop-up, OptionChooser.mxml:
<?xml version="1.0" encoding="utf-8"?> <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="338" height="252" title="Choose Your Theme" showCloseButton="true" horizontalAlign="center" verticalAlign="middle" close="PopUpManager.removePopUp(this)"> <mx:Metadata> [Event(name="changeOption", type="option.OptionEvent")] </mx:Metadata> <mx:Script> <![CDATA[ import option.OptionEvent; import mx.managers.PopUpManager; private function dispatchOptionChoice( optionName:String ) : void { dispatchEvent( new OptionEvent(optionName) ); PopUpManager.removePopUp(this); } ]]> </mx:Script> <mx:VBox horizontalAlign="left" verticalGap="17"> <mx:RadioButton label="option1" click="dispatchOptionChoice('option1')"/> <mx:RadioButton label="option2" click="dispatchOptionChoice('option2')"/> <mx:RadioButton label="option3" click="dispatchOptionChoice('option3')"/> </mx:VBox> </mx:TitleWindow>
When a RadioButton is clicked, an event is dispatched. The event is a OptionEvent - a custom event type. This is the code for the event, OptionEvent.as:
package option { import flash.events.Event; public class OptionEvent extends Event { public static const CHANGE_OPTION:String = "changeOption"; public var optionName:String; public function OptionEvent(optionName:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(CHANGE_OPTION, bubbles, cancelable); this.optionName = optionName; } } }
This is the code for the main application to demonstrate it:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" borderStyle="solid" borderColor="black" borderThickness="1"> <mx:Script> <![CDATA[ import option.OptionEvent; import option.OptionChooser; import mx.managers.PopUpManager; import mx.core.IFlexDisplayObject; private function showOptionDialog() : void { var pop:IFlexDisplayObject = PopUpManager.createPopUp(this, option.OptionChooser, true); pop.addEventListener( OptionEvent.CHANGE_OPTION, selectOption ); PopUpManager.centerPopUp(pop); result.text = ""; // reset } private function selectOption( event:OptionEvent ) : void { result.text = event.optionName; } ]]> </mx:Script> <mx:Button x="24" y="27" label="Select Option" click="showOptionDialog()"/> <mx:Label x="24" y="57" fontWeight="bold" color="#FFFFFF" id="result"/> </mx:Application>
A custom event seems to be the easiest solution for this 'problem'. It can even contain more information specific for your App but also contains the basic information from an Event because it Extends from the Event-class. If you have a lot of data (e.g., a user profile) consider creating a class to represent it and have the custom event contain an instance of the class. This also makes it easier to change the information later.
Source files
This zip file is a Flex Builder 3 project archive. If you do have Flex Builder 3, you can import the source files directly from the zip.















I am unable to understand this post. But well some points are useful for me.
Your blog is very interresting for me, i will come back here..
Just FYI, the link to the ZIP file is wrong. You have a \; between your name instead of a period:
matthijs\;stichelz instead of matthijs.stichelz.
Thanks for the post, I’m trying your sample code right now.
Thanks !!
Link is fixed , enjoy the example!!
Its really very cool solution.I got my problem solved.Very very thanks to author…