06
Aug
07

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.

View MXML (main.mxml)

<?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.


3 Responses to “Modules and singleton manager classes”


  1. 1 Valery Aug 7th, 2007 at 2:54 am

    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

  2. 2 Kaushik Datta Oct 26th, 2007 at 8:06 am

    Can you please post an example to show a ProgressBar while the Module SWF is getting loaded?

    TIA

  3. 3 rconceiver Jun 19th, 2008 at 9:47 pm

    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

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".