8 Replies Latest reply on Jun 16, 2011 12:42 PM by FM_Flame

    Flex 4.1 Do custom components export in Application or Module if used in the Module only?

    FM_Flame Level 1

      I think I am missing something here:

       

      "Because a module is loaded into a child domain, it owns class  definitions that are not in the main application's domain. For example,  the first module to load the PopUpManager class becomes the owner of the  PopUpManager class for the entire application because it registers the  manager with the SingletonManager. If another module later tries to use  the PopUpManager, Adobe ® Flash® Player throws an exception.

       

      This technique also applies to components. The module that first uses  the component owns that component's class definition in its domain. As a  result, if another module tries to use a component that has already  been used by another module, its definition will not match the existing  definition. To avoid a mismatch of component definitions, create an  instance of the component in the main application. The result is that  the definition of the component is owned by the main application and can  be used by modules in any child domain."

       

      I thought that if I use a component only in a module then I won't be able to use it in the application or sibling modules and I would be able to use it only in submodule of that module since the component definitions are in the module application domain which is a child of the application's application domain.

       

      So I've just made a simple example: application with 2 moduleLoaders and 2 modules both using the same 3 components:

      1) ButtonBar

      2) DataGrid

      3) CustomSkinnableComponent (empty, just extends SkinnableComponent) with custom skin (with a Rect 200x200 px, extends Skin).

       

      I start the application -> both modules load without problem. Wasn't this supposed to cause error with loading the second module because the first module has the components definitions? Or it doesn't cause an error because the component definitions match, and they match because my component is in the same flex project and in this case there is no problem. Problem will be if I have 2 or more RSLs with same package and class definitions but different functionality?

       

      I don't really understand how to cause mismatch of component definitions so I can watch out to avoid it.. can someone please explain ?

       

      I'm confused and I thought I knew this stuff. What am I missing ?

       

      Also one more thing:

      I thought that if a module holds the component definitions that means it's swf will grow in size -> reason for using modules. If the application holds the components definitions wouldn't this mean the application swf will grow in size and then -> why would I use a module? To save memory from instances and embeded images/fonts in module?

       

      Also having in mind that the skin to a component is also a class definition and I embed some large images in it, and I use that skin in 2 modules, wouldn't that require the skin to be put in the application -> large size of application swf and not module swf as it should be?

       

      Please someone spread some light in my foggy mind

       

      Message was edited by: FM_Flame

        • 1. Re: Flex 4.1 Do custom components export in Application or Module if used in the Module only?
          Flex harUI Adobe Employee

          First in wins, but sibling definitions are fine.  See the modules

          presentation on my blog

           

          --

          Alex Harui

          Flex SDK Team

          Adobe System, Inc.

          http://blogs.adobe.com/aharui

          • 2. Re: Flex 4.1 Do custom components export in Application or Module if used in the Module only?
            FM_Flame Level 1

            Ok I read it (maybe for 5th time) to recall things a bit. So let me summarize after reading testing etc what I still don't understand:

             

            1) In my application I have 1 custom skin extending Skin.

            import custom.CSCSkin;
            public var testSkinnn:CSCSkin;

             

            and on creation complete I have:

            var testSkin:Class = loaderInfo.applicationDomain.getDefinition("custom.CSCSkin") as Class;
            trace(testSkin);

             

            This returns [class CSCSkin].

             

            But the same thing doesn't work in the module because the loaderInfo is null.

             

            So how do you access the applicationDomain of a module ?

             

            2) Looks like I can use almost any component as you say in the siblings and there is no problem, except for the managers classes and Alert. I guess that's because they are singletons with static vars and functions and they register their reference only once - for one application domain and get restrained by it.

             

            I just tried to use Alert only in a module. Application is empty, just a moduleLoader.

             

            import mx.managers.PopUpManager;
            private var popUpManager:PopUpManager;
                       
            import mx.controls.Alert;
            private var alert:Alert;

             

            On creation complete I have:

            Alert.show("TESTING","bla");

             

            In the docs it says that registering the singletons/managers in module will make it throw error when you try to use it in a sibling module. But the fact is that the Alert doesn't work at all even in the module I registered them first - it throws error with the PopUpManager. To make the Alert work I had to move the Alert and PopUpManager declarations to the main application. Then they started working everywhere.

             

            Why the Alert is not working if used only in one module?

            • 3. Re: Flex 4.1 Do custom components export in Application or Module if used in the Module only?
              FM_Flame Level 1

              Oh and I forgot the most important questions I wanted to ask you:

               

              1) How can I cause mismatch of component definitions? Is that even possible within one flex project?

               

              2) I just even tried to have the same component package and name in a merged RSL as in the Flex Project and this didn't cause anything because I was unable to select the component from the RSL, it was like Flex Project one won over the one from the RSL. Is that correct ?

              • 5. Re: Flex 4.1 Do custom components export in Application or Module if used in the Module only?
                Flex harUI Adobe Employee

                Sorry, must have missed your earlier response.

                 

                Use the factory.create("custom.CSCSkin") to create instances of classes in

                the module.  Then you don't need applicationDomain references.

                 

                Hopefully the release notes for Flex 4 say that you have to put singleton

                managers in the main app.  It wasn't true for Flex 3.

                 

                You can cause a mismatch by passing references from one module to another

                module via shared singletons or some other mechanism.

                 

                First-in-wins.  I'm not sure how you set up your RSL test.

                1 person found this helpful
                • 6. Re: Flex 4.1 Do custom components export in Application or Module if used in the Module only?
                  FM_Flame Level 1

                  Hi, that's for the reply.

                   

                  Great, I got it for the module part and for the managers.

                   

                  Could you give me a simple example how to cause that mismatch? I didn't really get it, but if it's hard to do it you can skip that.

                   

                  I set up my RSL by creating a Flex Library project and a Flex Project. The Flex Library project has package custom with component CustomComponent. Then I add the compiled Library project's swc to the Flex project's library path with merged option. In my Flex project I also had package custom with component named CustomComponent. So I have the same component with the same package in my flex library and in my flex project and I could see only the one in my flex project. When I tested this both CustomComponent were 100% the same. Not sure what would have happened if they had different functions...

                  • 7. Re: Flex 4.1 Do custom components export in Application or Module if used in the Module only?
                    Flex harUI Adobe Employee

                    Roughly:

                     

                    <!-- main app -->

                    <mx:Application>

                        <mx:Script>

                            public var foo:Object;

                        </mx:Script>

                        <mx:Button click="ml1.url='mod1.swf'" />

                        <mx:Button click="ml2.url="mod1.swf'" />

                        <mx:ModuleLoader id="ml1" />

                        <mx:ModuleLoader id="ml2" />

                    </mx:Application>

                     

                    <!-- module -->

                    <mx:Module creationComplete="runTest()">

                        <mx:Script>

                            public function runTest():void

                            {

                                if (Application.application.foo == null)

                                    Application.application.foo = new SomeClass();

                                else

                                    trace(SomeClass(Application.application.foo));

                            }

                     

                    If you click the first button then the second, you should see a type

                    mismatch.

                    • 8. Re: Flex 4.1 Do custom components export in Application or Module if used in the Module only?
                      FM_Flame Level 1

                      Thanks a lot, I've got the idea   No more questions here I think