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.

 
 
About The Author

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.

7 Responses to Modules and singleton manager classes

  1. Valery says:

    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. Can you please post an example to show a ProgressBar while the Module SWF is getting loaded?

    TIA

  3. rconceiver says:

    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

  4. Dennis says:

    Thank you Peter!

    That was exactly what I need!

    Regards,
    Dennis

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

  6. Suresh Nagar says:

    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.

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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree