I’m working with flex module; I have a ModelLocator (Singleton) class
that populates the module with data. In my application I load a module
using the ModuleManager.getModlue(url) syntax. I create two instance’s
as “A” and “B” (using info.factory.create()) of a module and add the
same on the main application (using addChild) and data is passed to
module using its own ModelLocator (Singleton) class.
Now, the problem is that when I change the value of instance “A” same
value reflects on the instance “B”. This is happening because as I
change the value ModelLocator(Singleton class ) gets updated  which in
turns updates the instance "B" and vice versa.

How do I create two separate instances of a module (which has a
singleton ModelLocator) without loading a module again?

Below is the code:

Main Application:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009";
                           xmlns:s="library://ns.adobe.com/flex/spark"
                           xmlns:mx="library://ns.adobe.com/flex/halo" 
minWidth="1024"
minHeight="768"
                           initialize="application1_initializeHandler(event)" >
        <fx:Script>
                <![CDATA[
                        import com.ngi.manager.ReportLoadManager;

                        import mx.events.FlexEvent;
                        import mx.events.ModuleEvent;
                        import mx.modules.IModuleInfo;
                        import mx.modules.ModuleManager;

                        private var _objLeft:Object;
                        private var _objRight:Object;
                        public var info:IModuleInfo;

                        protected function 
button1_clickHandler(event:MouseEvent):void
                        {
                                switch(event.target.label)
                                {
                                        case "left":
                                                _objLeft.incrementAge();
                                                break;
                                        case "right":
                                                _objRight.incrementAge();
                                                break;
                                }
                        }

                        protected function
application1_initializeHandler(event:FlexEvent):void
                        {
                                info = ModuleManager.getModule("Module1.swf");
                                info.addEventListener(ModuleEvent.READY, 
modEventHandler);
                                info.load();
                        }

                        private function modEventHandler(e:ModuleEvent):void
                        {
                                _objLeft = info.factory.create();
                                _objRight = info.factory.create();

                                pLeft.addChild(_objLeft as DisplayObject);
                                pRight.addChild(_objRight as DisplayObject);
                        }
                ]]>
        </fx:Script>
        <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) 
here
-->
        </fx:Declarations>

        <s:VGroup>
                <s:HGroup>
                        <mx:Panel id="pLeft" />
                        <mx:Panel id="pRight" />
                </s:HGroup>

                <s:HGroup>
                        <s:Button label="left" 
click="button1_clickHandler(event)" />
                        <s:Button label="right" 
click="button1_clickHandler(event)" />
                </s:HGroup>
        </s:VGroup>
</s:Application>


Module:

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009";
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/halo" layout="absolute"
width="400" height="300">
        <fx:Script>
                <![CDATA[
                        import com.model.ModelLocator;
                        public function setAge(nAge:int):void
                        {
                                ModelLocator.getInstance().age = nAge;
                        }

                        public function incrementAge():void
                        {
                                ModelLocator.getInstance().age++;
                        }
                ]]>
        </fx:Script>
        <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) 
here
-->
        </fx:Declarations>
        <s:Label id="lblAge1" text="{ModelLocator.getInstance().age}"
fontSize="50" />
</mx:Module>

Model class of a module:

package com.model
{
        [Bindable]
        public class ModelLocator
        {
                private var _age:int = -1;
                static private var _instance:ModelLocator = null;

                public function ModelLocator()
                {
                        if(_instance!=null)
                                throw new Error("Singleton cannot be 
instantiated using
constructor, use getInstance method instead");
                }

                public static function getInstance():ModelLocator
                {
                        if(_instance == null)
                        {
                                _instance = new ModelLocator();
                        }
                        return _instance;
                }

                public function set age(nAge:int):void
                {
                        _age = nAge;
                }

                public function get age():int
                {
                        return _age;
                }
        }
}

-- 
You received this message because you are subscribed to the Google Groups "Flex 
India Community" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/flex_india?hl=en.

Reply via email to