How to use modules in the same project:
- Define an interface for your module as the following. The whole point of modular app is to program to abstract interfaces so that different parts of your app have less dependency on one another; it also makes it easier to test:
package
{
import flash.events.IEventDispatcher;public interface IInnerModuel1 extends IEventDispatcher
{
function foo():void
}
} - Create a new module by right-click on the project on the Package Explorer> New > MXML Module. Flex then generates a spark module (s:Module) for you. The module can has its own namespace; if it does, that means the resulting .swf file will be compiled into a subfolder that corresponds to its namespace structure, like “/debug-bin/com/mh/test/flex/modules/MyInnerModule1.swf” or something.
<s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
implements="IInnerModuel1"><fx:Script>
<![CDATA[
public function foo():void
{
myFooLabel.text = "Yo! What's up?";
}
]]>
</fx:Script>
...</s:Module>
- Add the module to the project by right-click > Project Properties > Flex Modules > Add and navigate to the .mxml or the .as component.
- Load the module for the main app to use. You can do this using the following tag in your main app:
<s:ModuleLoader id="innerModule1Loader" url="com/mh/test/flex/modules/MyInnerModule1.swf" />
- In order to access your module from main WindowedApplication, you can do this:
protected function button1_clickHandler(event:MouseEvent):void
{if(innerModule1Loader.child == null)
return;var iChild:* = innerModule1Loader.child as IInnerModuel1;
iChild.foo();}
Some links:
How to pass data to modules and access modules from main app and vice versa (Flex 4)
How to use modules from different Flex projects
Step by step of how to use modules in Flex 3