Flex Pasta » Modal Windows in MXML
Modal Windows in MXML
Up until the other day, when using modal and/or pop up windows in Flex, I have always instantiated them in Actionscript. Since I use relative layout, I couldn’t really add an mxml component that would be a modal without it being drawn on the screen. The advantages of declaring a modal in mxml would be:
- Taking advantage of mx:Metadata tags that allow me to invoke methods when events occur without manually adding listeners
- Automatic binding the same way my other components work
- Less code; by declaring the pop up window in mxml, I don’t have to invoke a method that creates an instance of the pop-up
Here is how you can declare a modal/popup window in mxml:
<mx:ArrayCollection id=”modals”>
<MyPopupWindowComponent id=”modal1″ someBindings=”{bindingAVariable}” anEventThisComponentDispatchesInMetaData=”doSomething(event)”/>
</mx:ArrayCollection>
Now when you want to open the modal, just do PopupManager.addPopup(modal1,true,DisplayObject(Application.application));
This allows you to use the ease and power of bindings/metadata in modal windows.
3 Comments
1. David R replies at 10th September 2009, 1:29 pm :
Just FYI, you can use binding in actionscript as well as in MXML. You just create the binding manually by using the global BindingUtils class.
2. ebaggg replies at 16th September 2009, 1:19 pm :
Isn’t this bad design though? Doing it in mxml you’re instantiating all these objects in memory that may never get used.
3. Brian Telintelo replies at 27th October 2009, 8:03 pm :
@David R Yes but the joy of bindings and MXML is just more fun than setting up bindings manually?
@ebaggg Yeah, it could be considered bad design. On the other hand though, if you open a modal window and close it, is your memory being released? If not, this approach will save on memory because it will use the same instance over and over again.
Leave a comment