You can use the ModuleLoaderclass
to load a module in an application or other module. The easiest
way to do this in an MXML application is to use the <s:ModuleLoader> tag.
You set the value of the url property to point
to the location of the module’s SWF file. The following example
loads the module when the application first starts:
<?xml version="1.0"?>
<!-- modules/MySimplestModuleLoader.mxml -->
<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/mx">
<s:ModuleLoader url="ColumnChartModule.swf"/>
</s:Application>
You can change the timing of when the module loads by setting
the value of the url property at some other time,
such as in response to an event. Setting the target URL of a ModuleLoader
object triggers a call to the loadModule() method.
This occurs when you first create a ModuleLoader object with the url property
set. It also occurs if you change the value of that property.
If you set the value of the url property to
an empty string ("") or null,
the ModuleLoader object unloads the current module by calling the release() method.
You can have multiple instances of the ModuleLoader class in
a single application. The following example loads the modules when
the user navigates to the appropriate tabs in the TabNavigator container:
<?xml version="1.0"?>
<!-- modules/URLModuleLoaderApp.mxml -->
<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/mx">
<s:Panel title="Module Example" width="100%" height="100%">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<mx:TabNavigator id="tn"
paddingTop="10"
paddingLeft="10"
paddingRight="10"
paddingBottom="10"
width="100%" height="100%"
creationPolicy="auto">
<s:ModuleLoader id="ml1"
label="ColumnChart Module"
url="ColumnChartModule.swf"/>
<s:ModuleLoader id="ml2"
label="BarChart Module"
url="BarChartModule.swf"/>
</mx:TabNavigator>
</s:Panel>
</s:Application>
You can also use the ModuleLoader API to load and unload modules
with the loadModule() and unloadModule() methods.
These methods take no parameters; the ModuleLoader class loads or
unloads the module that matches the value of the current url property.
The following example loads and unloads the module when you click
the button:
<?xml version="1.0"?>
<!-- modules/ASModuleLoaderApp.mxml -->
<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/mx">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import spark.modules.ModuleLoader;
public function createModule(m:ModuleLoader, s:String):void {
if (!m.url) {
m.url = s;
}
m.loadModule();
}
public function removeModule(m:ModuleLoader):void {
m.unloadModule();
}
]]>
</fx:Script>
<s:Panel title="Module Example" width="100%" height="100%">
<mx:TabNavigator id="tn"
paddingTop="10"
paddingLeft="10"
paddingRight="10"
paddingBottom="10"
width="100%" height="100%"
creationPolicy="auto">
<s:NavigatorContent label="ColumnChartModule">
<s:layout>
<s:VerticalLayout paddingTop="10" paddingLeft="5"/>
</s:layout>
<s:Button label="Load"
click="createModule(chartModuleLoader, l1.text)"/>
<s:Button label="Unload"
click="removeModule(chartModuleLoader)"/>
<s:Label id="l1" text="ColumnChartModule.swf"/>
<s:ModuleLoader id="chartModuleLoader"/>
</s:NavigatorContent>
<s:NavigatorContent label="FormModule">
<s:layout>
<s:VerticalLayout paddingTop="10" paddingLeft="5"/>
</s:layout>
<s:Button label="Load"
click="createModule(formModuleLoader, l2.text)"/>
<s:Button label="Unload"
click="removeModule(formModuleLoader)"/>
<s:Label id="l2" text="FormModule.swf"/>
<s:ModuleLoader id="formModuleLoader"/>
</s:NavigatorContent>
</mx:TabNavigator>
</s:Panel>
</s:Application>
When you load a module, Flex ensures that there is only one copy
of a module loaded, no matter how many times you call the load() method
for that module.