Modules and singleton manager classes

by Peter deHaan on August 6, 2007

in DragManager, ModuleLoader, Modules, PopUpManager, TabNavigator

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.

{ 7 comments… read them below or add one }

1 Valery August 7, 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

Reply

2 Kaushik Datta October 26, 2007 at 8:06 am

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

TIA

Reply

3 rconceiver June 19, 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

Reply

4 Dennis May 7, 2009 at 4:37 am

Thank you Peter!

That was exactly what I need!

Regards,
Dennis

Reply

5 Jelger Muylaert June 5, 2009 at 5:07 pm

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.

Reply

6 Suresh Nagar December 4, 2009 at 12:09 am

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.

Reply

7 Andrea Bresolin March 12, 2010 at 2:36 pm

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/

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: