Modules and singleton manager classes
This is a bit more of a “gotcha” than a tip, but it is something I’ve run into twice in the past week or so. When working with application domains and singletons (such as the DragManager or PopUpManager). I’ve been playing with modules the past couple days and ran into an issue when trying to drag items from a DataGrid in one module to a DataGrid in the second module. When trying to select an item in one of the data grids, I’d get strange run-time errors. The solution? Create a reference in my main application to a dummy DragManager or PopUpManager instance.
Hopefully this will save somebody a little bit of a headache in the future.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import mx.managers.DragManager;
/* Create dummy variables. */
private var dragManager:DragManager;
private var popUpManager:PopUpManager;
]]>
</mx:Script>
<mx:TabNavigator id="tabNavigator" width="300" height="200">
<mx:ModuleLoader id="module1" url="dataGrid_module.swf" label="Tab 1" />
<mx:ModuleLoader id="module2" url="comboBox_module.swf" label="Tab 2" />
</mx:TabNavigator>
</mx:Application>
View MXML (dataGrid_module.mxml)
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="100%" height="100%">
<mx:Style>
Module {
padding-bottom: 5px;
padding-left: 5px;
padding-right: 5px;
padding-top: 5px;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</mx:Script>
<mx:VBox height="100%">
<mx:DataGrid id="dataGrid" width="100%" height="100%">
<mx:dataProvider>
<mx:Object data="1" label="one" />
<mx:Object data="2" label="two" />
<mx:Object data="3" label="three" />
<mx:Object data="4" label="four" />
</mx:dataProvider>
</mx:DataGrid>
</mx:VBox>
<mx:HRule width="100%" />
<mx:Button label="Alert.show(...)" click="Alert.show('Alert from the dataGrid_module.')" />
</mx:Module>
View MXML (comboBox_module.mxml)
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="100%" height="100%">
<mx:Style>
Module {
padding-bottom: 5px;
padding-left: 5px;
padding-right: 5px;
padding-top: 5px;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</mx:Script>
<mx:VBox height="100%">
<mx:ComboBox id="comboBox">
<mx:dataProvider>
<mx:Object data="1" label="one" />
<mx:Object data="2" label="two" />
<mx:Object data="3" label="three" />
<mx:Object data="4" label="four" />
</mx:dataProvider>
</mx:ComboBox>
</mx:VBox>
<mx:HRule width="100%" />
<mx:Button label="Alert.show(...)" click="Alert.show('Alert from the comboBox_module.')" />
</mx:Module>
View source is enabled in the following example.
For more information see the following bugs in the public Flex Bugbase: SDK-9354 and SDK-11272.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
7 Responses to Modules and singleton manager classes
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Interesting, I have some strange issue with modules and styles — probably should apply this workaround to StyleManager.
Btw, I’d prefer the following form of linking classes:
import mx.managers.PopUpManager;
import mx.managers.DragManager;
private static const LINKAGE:Array = [DragManager, PopUpManager];
First, you save a bit on characters typed. Second, you don’t pollute instance with unnecessary variables, so you save on object size and amount of scroll clicks during debugging ;)
Valery
Can you please post an example to show a ProgressBar while the Module SWF is getting loaded?
TIA
Thanks once again Peter…:)
I was facing the same issue. Initially I thought it is occurring because the last Module is not been unloaded properly. So i tried with unloadModule(). But it didn’t work.
Finally I got this solution! Thanks for that…
But can you help me understand why this issue occurs. Is there anything wrong with the Module Child or something else.
Awaiting for the reply.
rconceiver
Thank you Peter!
That was exactly what I need!
Regards,
Dennis
By creating the dummy Managers, you ‘activate’ them at the beginning of your application. If you don’t ‘activate’ them soon enough, you get strange errors indeed.
Hi Jelger,
i think there is more to it.
I have two modules and one GlobalModel (singleton class).
in both the modules i am calling getInstance of the GlobalModel and setting values to the model.
The changes do reflect in both the modules through global model only if i declare/instantiate the GlobalModel in main file. If i do not do that both modules share separate instances, infact, it gets reset when module is loaded second time.
Suresh is right. With modules you can have a module-wide singleton or an application-wide singleton depending on whether you import the singleton class also in the main application or not. I’ve written a post on my blog about this that demonstrates the different behaviors. You can check it at http://www.devahead.com/blog/2010/03/beware-of-singleton-in-flex-modules/