[url]http://nianshi.iteye.com/blog/743822[/url]
Module加载方式概览
在理解这种加载方式之前,我们应该首先知道什么是Module模块。Module 实际上是一个预编译的SWF文件。虽然是SWF格式的文件,但是这个文件不能独立运行,并且只能被ModuleLoader加载后才能显示。逻辑上它是一个容器,可以像一般的容器一样包含别的容器,组件,甚至是别的Module模块。根据需要,预编译的Module模块可以被应用加载和卸载。不同的应用可以共享这些Module模块。Flex应用可以被分割为若干个预编译的Module模块,可以在需要时分别加载这些模块,避免在系统初始化时加载全部子容器。采用这种方式的Flex应用从设计上分隔了逻辑相对独立的模块,减少了系统初始化时的加载时间。
Module的创建、编译、加载和卸载方法
我们有两种方式创建一个Module模块,一是利用MXML标签<mx:Module>创建Module类;令一种方式采用 ModuleManager类在ActionScript中创建Module模块类。模块类创建后将被编译成SWF文件。如前面章节所述我们可以利用 mxmlc编译器手动编译或者在Flex Builder3集成开发环境提供的工具自动编译Module类为SWF文件。这里介绍采用Flex Builder3集成开发环境提供的方式创建一个Module类。
在FlexBuilder3中创建基于MXML标签的Module模块
创建基于MXML标签的Module模块,需要扩展mx.modules.Module.
1. 第一步,在Flex Builder3集成开发环境中创建一个Flex项目Moduler。
2. 第二步,选择FileàNewàMXML Module
3. 第三步,输入Module文件名字为MXMLDemoModule,设置Module的高度和宽度,选择Module容器的布局方式为absolute.,选择默认的预编译后优化的SWF选项,单击Finish按钮。
4. Module文件被创建,编辑Module文件,在此例中我们可以假定是一个登陆Module界面,
其源代码为,
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
4.
5. <mx:Panel width="300" height="220" layout="absolute" horizontalCenter="0"
6.
7. verticalCenter="0" backgroundAlpha="0.5">
8.
9. <mx:Form horizontalCenter="0" verticalCenter="0" paddingBottom="0" paddingLeft="0" paddingRight="0"
10.
11. paddingTop="0">
12.
13. <mx:FormItem label="Account" indicatorGap="5">
14.
15. <mx:TextInput borderStyle="solid" id="account" text="admin"/>
16.
17. </mx:FormItem>
18.
19. <mx:FormItem label="Password" indicatorGap="5">
20.
21. <mx:TextInput borderStyle="solid" id="password" text="admin" displayAsPassword="true"/>
22.
23. </mx:FormItem>
24.
25. <mx:FormItem direction="horizontal" >
26.
27. <mx:Button id="logined" label="Login" styleName="blueButton"/>
28.
29. <mx:Button id="reset" label="Reset" styleName="greenButton"/>
30.
31. </mx:FormItem>
32.
33. </mx:Form>
34.
35. </mx:Panel>
36.
37. <mx:Label text="CopyRight 2008 demo Solutions" horizontalCenter="0" verticalCenter="252.5"/>
38.
39. </mx:Module>
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
<mx:Panel width="300" height="220" layout="absolute" horizontalCenter="0"
verticalCenter="0" backgroundAlpha="0.5">
<mx:Form horizontalCenter="0" verticalCenter="0" paddingBottom="0" paddingLeft="0" paddingRight="0"
paddingTop="0">
<mx:FormItem label="Account" indicatorGap="5">
<mx:TextInput borderStyle="solid" id="account" text="admin"/>
</mx:FormItem>
<mx:FormItem label="Password" indicatorGap="5">
<mx:TextInput borderStyle="solid" id="password" text="admin" displayAsPassword="true"/>
</mx:FormItem>
<mx:FormItem direction="horizontal" >
<mx:Button id="logined" label="Login" styleName="blueButton"/>
<mx:Button id="reset" label="Reset" styleName="greenButton"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
<mx:Label text="CopyRight 2008 demo Solutions" horizontalCenter="0" verticalCenter="252.5"/>
</mx:Module>
集成登陆Module模块到主应用文件ModulerApp.mxml,
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
4.
5. layout="absolute">
6.
7. <mx:ModuleLoader url="MXMLDemoModule.swf"/>
8.
9. </mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:ModuleLoader url="MXMLDemoModule.swf"/>
</mx:Application>
创建基于ActionScript的Module模块
在ActionScript中创建的Module模块类大都继承了mx.modules.Module或者 mx.modules.ModuleBase基础类。我们都知道,MXML标签<mx:Module>实际上是 mx.modules.Module类的另一种表现方式,这也就不难理解无论在MXML标签中还是在ActionScript中创建Module模块其实都是相通的。自定义Module类继承mx.modules.Module和继承mx.modules.ModuleBase两个基类的不同是,继承自前者的或者基于<mx:Module>标签的自定义Module组件将被加入到框架可视化显示列表,后者则没有。参考示例
Java代码
1. package {
2.
3. import mx.modules.ModuleBase;
4.
5. public class MyModule extends ModuleBase {
6.
7. public function MyModule() {
8.
9. trace("MyModule was created!");
10.
11. }
12.
13. }
14.
15. }
package {
import mx.modules.ModuleBase;
public class MyModule extends ModuleBase {
public function MyModule() {
trace("MyModule was created!");
}
}
}
编译Module模块
前面已经提及我们有两种方式来编译Module模块文件。像编译主应用文件一样,一种是在命令行手动编译,最简单的情形 可以利用这个命令
Mxmlc MyModule.mxml
另一种是在Flex Builder3中提供的自动编译工具编译。编译的结果是一个可以被装载的SWF文件,与编译后的SWF主应用文件最大的不同就是,模块SWF文件不能独立运行,只能在被装载后才能够和其宿主共同运行。
怎样装载和卸载Module模块
总体来说,有几种方式可以实现Module模块的装载和卸载。
利用ModuleLoader:ModuleLoader类提供了一系列高层处理Module的编程接口。
利用ModuleManager: ModuleManager类提供了低层次的处理Module的装载卸载以及事件响应等的变成接口。
下面将详细地讲解这两种处理方式。
我们可以利用ModuleLoader类在主应用文件或者别的Module模块中加载任意预编译的Module对象。最简单的方式是采用 ModuleLoader类的标签形式<mx:ModuleLoader>在主应用的MXML文件中显式地加载Module,然后只需要设置这个标签的url属性为预编译Module的SWF文件位置,可以是相对路径,也可以是绝对路径。参考实例,
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
4.
5. layout="absolute">
6.
7. <mx:ModuleLoader url="MXMLDemoModule.swf"/>
8.
9. </mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:ModuleLoader url="MXMLDemoModule.swf"/>
</mx:Application>
编程时我们可以根据需要替换此url所指向的模块位置,实现不同模块的更迭显示。当ModuleLoader类初始化时或者每次更改这个url属性值时,这个ModuleLoader类的loadModule()方法被触发。如果设定url属性为空字符串,ModuleLoader卸载当前加载的 Module模块。
ModuleLoader其实是一种特殊的导航式容器。和一般导航式容器如ViewStack不同的是,ModuleLoader不必在初始化时携带加载所有的孩子组件。了解这一点,我们可以猜想到,在一个Flex主应用中可以包含甚至嵌套包含多个ModuleLoader实例,以下示例展示在一个ViewStack容器中包含多个ModuleLoader的示例。
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Panel
6.
7. title="Multiple Modules Demo "
8.
9. height="100%"
10.
11. width="100%"
12.
13. paddingTop="0"
14.
15. paddingLeft="0"
16.
17. paddingRight="0"
18.
19. paddingBottom="0"
20.
21. >
22.
23. <mx:VBox id="v1" label="Module1">
24.
25. <mx:Label id="l1" text="Module1.swf"/>
26.
27. <mx:ModuleLoader url="Module1.swf"/>
28.
29. </mx:VBox>
30.
31.
32.
33. <mx:VBox id="v2" label="Module2">
34.
35. <mx:Label id="l2" text="Module2.swf"/>
36.
37. <mx:ModuleLoader url="Module2.swf"/>
38.
39. </mx:VBox>
40.
41.
42.
43. <mx:VBox id="v3" label="Module3">
44.
45. <mx:Label id="l3" text="Module3.swf"/>
46.
47. <mx:ModuleLoader url="Module3.swf"/>
48.
49. </mx:VBox>
50.
51. </mx:TabNavigator>
52.
53. </mx:Panel>
54.
55. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Panel
title="Multiple Modules Demo "
height="100%"
width="100%"
paddingTop="0"
paddingLeft="0"
paddingRight="0"
paddingBottom="0"
>
<mx:VBox id="v1" label="Module1">
<mx:Label id="l1" text="Module1.swf"/>
<mx:ModuleLoader url="Module1.swf"/>
</mx:VBox>
<mx:VBox id="v2" label="Module2">
<mx:Label id="l2" text="Module2.swf"/>
<mx:ModuleLoader url="Module2.swf"/>
</mx:VBox>
<mx:VBox id="v3" label="Module3">
<mx:Label id="l3" text="Module3.swf"/>
<mx:ModuleLoader url="Module3.swf"/>
</mx:VBox>
</mx:TabNavigator>
</mx:Panel>
</mx:Application>
同时我们可以利用ModuleLoader类提供的loadModule()和unloadModule()方法动态地指定被加载的Module 模块。这两个方法没有输入形参,调用时分别加载和卸载当前ModuleLoader实例url属性所指向的Module模块。下面示例展示了在 TabNavigator容器中,按钮点击后分别加载卸载不同的Module模块
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Script>
6.
7. <![CDATA[
8.
9. import mx.modules.*;
10.
11. public function moduleLoader(m:ModuleLoader, s:String):void {
12.
13. if (!m.url) {
14.
15. m.url = s;
16.
17. return;
18.
19. }
20.
21. m.loadModule();
22.
23. }
24.
25. public function moduleUnloader(m:ModuleLoader):void {
26.
27. m.unloadModule();
28.
29. }
30.
31. ]]>
32.
33. </mx:Script>
34.
35. <mx:Panel title="Dynamically load/unload multiple Modules"
36.
37. height="100%"
38.
39. width="100%"
40.
41. paddingTop="0"
42.
43. paddingLeft="0"
44.
45. paddingRight="0"
46.
47. paddingBottom="0"
48.
49. >
50.
51. <mx:TabNavigator id="navigator"
52.
53. width="100%"
54.
55. height="100%"
56.
57. creationPolicy="all"
58.
59. >
60.
61. <mx:VBox id="vb1" label="Module1">
62.
63. <mx:Button label="Load" click="moduleLoader(moduleLoader, l1.text)" />
64.
65. <mx:Button label="Unload" click="moduleUnloader(moduleLoader)" />
66.
67. <mx:Label id="l1" text="Module1.swf"/>
68.
69. <mx:ModuleLoader id="moduleLoader"/>
70.
71. </mx:VBox>
72.
73.
74.
75. <mx:VBox id="vb2" label="Module2">
76.
77. <mx:Button label="Load" click="moduleLoader(moduleLoader2, l2.text)"/>
78.
79. <mx:Button label="Unload" click="removeModule(moduleLoader2)" />
80.
81. <mx:Label id="l2" text="Module2.swf"/>
82.
83. <mx:ModuleLoader id="moduleLoader2"/>
84.
85. </mx:VBox>
86.
87.
88.
89. <mx:VBox id="vb3" label="Module3">
90.
91. <mx:Button label="Load" click="moduleLoader(moduleLoader3, l3.text)"/>
92.
93. <mx:Button label="Unload" click="removeModule(moduleLoader3)" />
94.
95. <mx:Label id="l3" text="Module3.swf"/>
96.
97. <mx:ModuleLoader id="moduleLoader3"/>
98.
99. </mx:VBox>
100.
101.
102.
103. </mx:TabNavigator>
104.
105. </mx:Panel>
106.
107. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.modules.*;
public function moduleLoader(m:ModuleLoader, s:String):void {
if (!m.url) {
m.url = s;
return;
}
m.loadModule();
}
public function moduleUnloader(m:ModuleLoader):void {
m.unloadModule();
}
]]>
</mx:Script>
<mx:Panel title="Dynamically load/unload multiple Modules"
height="100%"
width="100%"
paddingTop="0"
paddingLeft="0"
paddingRight="0"
paddingBottom="0"
>
<mx:TabNavigator id="navigator"
width="100%"
height="100%"
creationPolicy="all"
>
<mx:VBox id="vb1" label="Module1">
<mx:Button label="Load" click="moduleLoader(moduleLoader, l1.text)" />
<mx:Button label="Unload" click="moduleUnloader(moduleLoader)" />
<mx:Label id="l1" text="Module1.swf"/>
<mx:ModuleLoader id="moduleLoader"/>
</mx:VBox>
<mx:VBox id="vb2" label="Module2">
<mx:Button label="Load" click="moduleLoader(moduleLoader2, l2.text)"/>
<mx:Button label="Unload" click="removeModule(moduleLoader2)" />
<mx:Label id="l2" text="Module2.swf"/>
<mx:ModuleLoader id="moduleLoader2"/>
</mx:VBox>
<mx:VBox id="vb3" label="Module3">
<mx:Button label="Load" click="moduleLoader(moduleLoader3, l3.text)"/>
<mx:Button label="Unload" click="removeModule(moduleLoader3)" />
<mx:Label id="l3" text="Module3.swf"/>
<mx:ModuleLoader id="moduleLoader3"/>
</mx:VBox>
</mx:TabNavigator>
</mx:Panel>
</mx:Application>
我们当然也可以利用ModuleManager类来加载Module模块。这种方式比起纯粹的ModuleLoader方式稍微复杂一点,但是 ModuleManager提供了比ModuleLoader更加强大的能力来管理Module模块的加载过程。具体操作可以分成以下几步
通过ModuleManager实例的getModule()方法拿到Module模块的一个索引,索引类型为IModuleInfo。
调用这个索引的load()方法。
利用这个接口的factory属性拿到它相关连的Module工厂,调用此工厂的create()方法,并将返回值强制转换成当前的Module类型。
参考示例
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
4.
5. <mx:Script>
6.
7. <![CDATA[
8.
9. import mx.events.ModuleEvent;
10.
11. import mx.modules.ModuleManager;
12.
13. import mx.modules.IModuleInfo;
14.
15. public var info:IModuleInfo;
16.
17. private function init():void {
18.
19. info = ModuleManager.getModule("Module1.swf");
20.
21. info.addEventListener(ModuleEvent.READY, moduleEventHandler);
22.
23. info.load();
24.
25. }
26.
27. private function moduleEventHandler(e:ModuleEvent):void {
28.
29. vb.addChild(info.factory.create() as Module1);
30.
31. }
32.
33. ]]>
34.
35. </mx:Script>
36.
37. <mx:VBox id="vb"/>
38.
39. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import mx.modules.ModuleManager;
import mx.modules.IModuleInfo;
public var info:IModuleInfo;
private function init():void {
info = ModuleManager.getModule("Module1.swf");
info.addEventListener(ModuleEvent.READY, moduleEventHandler);
info.load();
}
private function moduleEventHandler(e:ModuleEvent):void {
vb.addChild(info.factory.create() as Module1);
}
]]>
</mx:Script>
<mx:VBox id="vb"/>
</mx:Application>
理论上说,Flex应用第一次启动时初始化下载的大小,集成了Module模块的应用相对比没有集成Module模块的相似应用要小一些。这减少了初始化页面的等待时间。甚至在Module模块的SWF文件还没有下载完毕的时候,主应用文件也可以顺利显示。Module模块第一次加载时将被缓存在客户端IE, 已缓存的Module模块被再次加载时,FlashPlayer将直接在缓存中加载Module实例,减少了加载时间,提高了用户体验。当然这有一个前提就是,客户端IE没有被清空。所以在实际操作中,你可以在SWF模块真正的被使用之前将其预加载到客户端缓存。方法是调用 ModuleManager.getModule("Module1.swf").load()语句。在调用create()方法之前并没有创建这个模块实例,而只是纯粹地将模块SWF文件加载到客户端内存。下面实例展示了在系统初始化时预先加载一个名字叫做Module1.swf的模块,当此模块需要显示时才创建这个Module模块实例。
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
4.
5. creationComplete="modulePreloader()">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. import mx.events.ModuleEvent;
12.
13. import mx.modules.ModuleManager;
14.
15. import mx.modules.IModuleInfo;
16.
17. private function modulePreloader():void {
18.
19. var info:IModuleInfo = ModuleManager.getModule("Module1.swf");
20.
21. info.addEventListener(ModuleEvent.READY, moduleEventHandler);
22.
23. info.load();
24.
25. }
26.
27. private function moduleEventHandler(e:ModuleEvent):void {
28.
29. trace('' + e.type);
30.
31. }
32.
33. ]]>
34.
35. </mx:Script>
36.
37. <mx:Panel title="Module Preloader" height="100%" width="100%" >
38.
39. <mx:TabNavigator id="tn" width="100%" height="100%" creationPolicy="all" >
40.
41. <mx:VBox id="vb1" label="Module1">
42.
43. <mx:ModuleLoader url="Module1.swf"/>
44.
45. </mx:VBox>
46.
47. </mx:TabNavigator>
48.
49. </mx:Panel>
50.
51. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="modulePreloader()">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import mx.modules.ModuleManager;
import mx.modules.IModuleInfo;
private function modulePreloader():void {
var info:IModuleInfo = ModuleManager.getModule("Module1.swf");
info.addEventListener(ModuleEvent.READY, moduleEventHandler);
info.load();
}
private function moduleEventHandler(e:ModuleEvent):void {
trace('' + e.type);
}
]]>
</mx:Script>
<mx:Panel title="Module Preloader" height="100%" width="100%" >
<mx:TabNavigator id="tn" width="100%" height="100%" creationPolicy="all" >
<mx:VBox id="vb1" label="Module1">
<mx:ModuleLoader url="Module1.swf"/>
</mx:VBox>
</mx:TabNavigator>
</mx:Panel>
</mx:Application>
怎样处理ModuleLoader事件
我们不仅可以以上述的方式加载卸载预编译Module模块,而且更进一步,也可以捕捉和处理模块在加载卸载过程中可能会触发的各种事件。这些事件由ModuleLoader触发,分别有setup,ready,loading,unload,progress,error和urlChanged 等。很好的利用这些内置的事件可以使我们更加精细地跟踪模块加载卸载过程,实现更加强大的功能。下述示例展示了一个自定义的ModuleLoader组件 CustomModuleLoader,这个组件会跟踪当模块实例被主应用加载时所有触发的事件。
Java代码
1. <?xml version="1.0" encoding="iso-8859-1"?>
2.
3. <mx:ModuleLoader xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
4.
5. creationComplete="init()">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. public function init():void {
12.
13. addEventListener("urlChanged", onUrlChanged);
14.
15. addEventListener("loading", onLoading);
16.
17. addEventListener("progress", onProgress);
18.
19. addEventListener("setup", onSetup);
20.
21. addEventListener("ready", onReady);
22.
23. addEventListener("error", onError);
24.
25. addEventListener("unload", onUnload);
26.
27. standin = panel;
28.
29. removeChild(standin);
30.
31. }
32.
33. public function onUrlChanged(event:Event):void {
34.
35. if (url == null) {
36.
37. if (contains(standin))
38.
39. removeChild(standin);
40.
41. } else {
42.
43. if (!contains(standin))
44.
45. addChild(standin);
46.
47. }
48.
49. progress.indeterminate=true;
50.
51. unload.enabled=false;
52.
53. reload.enabled=false;
54.
55. }
56.
57. public function onLoading(event:Event):void {
58.
59. progress.label="Loading module " + url;
60.
61. if (!contains(standin))
62.
63. addChild(standin);
64.
65. progress.indeterminate=true;
66.
67. unload.enabled=false;
68.
69. reload.enabled=false;
70.
71. }
72.
73. public function onProgress(event:Event):void {
74.
75. progress.label="Loaded %1 of %2 bytes...";
76.
77. progress.indeterminate=false;
78.
79. unload.enabled=true;
80.
81. reload.enabled=false;
82.
83. }
84.
85.
86.
87. public function onSetup(event:Event):void {
88.
89. progress.label="Module " + url + " initialized!";
90.
91. progress.indeterminate=false;
92.
93. unload.enabled=true;
94.
95. reload.enabled=true;
96.
97. }
98.
99. public function onReady(event:Event):void {
100.
101. progress.label="Module " + url + " successfully loaded!";
102.
103. unload.enabled=true;
104.
105. reload.enabled=true;
106.
107. if (contains(standin))
108.
109. removeChild(standin);
110.
111. }
112.
113. public function onError(event:Event):void {
114.
115. progress.label="Error loading module " + url;
116.
117. unload.enabled=false;
118.
119. reload.enabled=true;
120.
121. }
122.
123. public function onUnload(event:Event):void {
124.
125. if (url == null) {
126.
127. if (contains(standin))
128.
129. removeChild(standin);
130.
131. } else {
132.
133. if (!contains(standin))
134.
135. addChild(standin);
136.
137. }
138.
139. progress.indeterminate=true;
140.
141. progress.label="Module " + url + " was unloaded!";
142.
143. unload.enabled=false;
144.
145. reload.enabled=true;
146.
147. }
148.
149. public var standin:DisplayObject;
150.
151. ]]>
152.
153. </mx:Script>
154.
155. <mx:Panel id="panel" width="100%">
156.
157. <mx:ProgressBar width="100%" id="progress" source="{this}"/>
158.
159. <mx:HBox width="100%">
160.
161. <mx:Button id="unload"
162.
163. label="Unload Module"
164.
165. click="unloadModule()"
166.
167. />
168.
169. <mx:Button id="reload"
170.
171. label="Reload Module"
172.
173. click="unloadModule();loadModule()"
174.
175. />
176.
177. </mx:HBox>
178.
179. </mx:Panel>
180.
181. </mx:ModuleLoader>
182.
183.
184.
185.
186.
187. <?xml version="1.0"?>
188.
189. <mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
190.
191. <mx:Script>
192.
193. <![CDATA[
194.
195. [Bindable]
196.
197. public var selectedItem:Object;
198.
199. ]]>
200.
201. </mx:Script>
202.
203. <mx:ComboBox
204.
205. width="215"
206.
207. labelField="label"
208.
209. close="selectedItem=ComboBox(event.target).selectedItem"
210.
211. >
212.
213. <mx:dataProvider>
214.
215. <mx:Object label="Select Coverage"/>
216.
217. <mx:Object
218.
219. label="Life Insurance"
220.
221. module="insurancemodules/LifeInsurance.swf"
222.
223. />
224.
225. <mx:Object
226.
227. label="Auto Insurance"
228.
229. module="insurancemodules/AutoInsurance.swf"
230.
231. />
232.
233. <mx:Object
234.
235. label="Home Insurance"
236.
237. module="insurancemodules/HomeInsurance.swf"
238.
239. />
240.
241. </mx:dataProvider>
242.
243. </mx:ComboBox>
244.
245. <mx:Panel width="100%" height="100%">
246.
247. <CustomModuleLoader id="mod"
248.
249. width="100%"
250.
251. url="{selectedItem.module}"
252.
253. />
254.
255. </mx:Panel>
256.
257. <mx:HBox>
258.
259. <mx:Button label="Unload" click="mod.unloadModule()"/>
260.
261. <mx:Button label="Nullify" click="mod.url = null"/>
262.
263. </mx:HBox>
264.
265. </mx:Application>
<?xml version="1.0" encoding="iso-8859-1"?>
<mx:ModuleLoader xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
creationComplete="init()">
<mx:Script>
<![CDATA[
public function init():void {
addEventListener("urlChanged", onUrlChanged);
addEventListener("loading", onLoading);
addEventListener("progress", onProgress);
addEventListener("setup", onSetup);
addEventListener("ready", onReady);
addEventListener("error", onError);
addEventListener("unload", onUnload);
standin = panel;
removeChild(standin);
}
public function onUrlChanged(event:Event):void {
if (url == null) {
if (contains(standin))
removeChild(standin);
} else {
if (!contains(standin))
addChild(standin);
}
progress.indeterminate=true;
unload.enabled=false;
reload.enabled=false;
}
public function onLoading(event:Event):void {
progress.label="Loading module " + url;
if (!contains(standin))
addChild(standin);
progress.indeterminate=true;
unload.enabled=false;
reload.enabled=false;
}
public function onProgress(event:Event):void {
progress.label="Loaded %1 of %2 bytes...";
progress.indeterminate=false;
unload.enabled=true;
reload.enabled=false;
}
public function onSetup(event:Event):void {
progress.label="Module " + url + " initialized!";
progress.indeterminate=false;
unload.enabled=true;
reload.enabled=true;
}
public function onReady(event:Event):void {
progress.label="Module " + url + " successfully loaded!";
unload.enabled=true;
reload.enabled=true;
if (contains(standin))
removeChild(standin);
}
public function onError(event:Event):void {
progress.label="Error loading module " + url;
unload.enabled=false;
reload.enabled=true;
}
public function onUnload(event:Event):void {
if (url == null) {
if (contains(standin))
removeChild(standin);
} else {
if (!contains(standin))
addChild(standin);
}
progress.indeterminate=true;
progress.label="Module " + url + " was unloaded!";
unload.enabled=false;
reload.enabled=true;
}
public var standin:DisplayObject;
]]>
</mx:Script>
<mx:Panel id="panel" width="100%">
<mx:ProgressBar width="100%" id="progress" source="{this}"/>
<mx:HBox width="100%">
<mx:Button id="unload"
label="Unload Module"
click="unloadModule()"
/>
<mx:Button id="reload"
label="Reload Module"
click="unloadModule();loadModule()"
/>
</mx:HBox>
</mx:Panel>
</mx:ModuleLoader>
<?xml version="1.0"?>
<mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var selectedItem:Object;
]]>
</mx:Script>
<mx:ComboBox
width="215"
labelField="label"
close="selectedItem=ComboBox(event.target).selectedItem"
>
<mx:dataProvider>
<mx:Object label="Select Coverage"/>
<mx:Object
label="Life Insurance"
module="insurancemodules/LifeInsurance.swf"
/>
<mx:Object
label="Auto Insurance"
module="insurancemodules/AutoInsurance.swf"
/>
<mx:Object
label="Home Insurance"
module="insurancemodules/HomeInsurance.swf"
/>
</mx:dataProvider>
</mx:ComboBox>
<mx:Panel width="100%" height="100%">
<CustomModuleLoader id="mod"
width="100%"
url="{selectedItem.module}"
/>
</mx:Panel>
<mx:HBox>
<mx:Button label="Unload" click="mod.unloadModule()"/>
<mx:Button label="Nullify" click="mod.url = null"/>
</mx:HBox>
</mx:Application>
应用error事件
使用ModuleLoader的error事件可以允许开发者在当不知什么原因Module模块没有被加载或卸载成功的时候,做一些必要的动作。参考以下示例
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Script>
6.
7. <![CDATA[
8.
9. import mx.events.ModuleEvent;
10.
11. import mx.modules.*;
12.
13. import mx.controls.Alert;
14.
15. private function errorHandler(e:ModuleEvent):void {
16.
17. Alert.show("There was an error loading the module." +
18.
19. " Please contact the Help Desk.");
20.
21. trace(e.errorText);
22.
23. }
24.
25. public function createModule():void {
26.
27. if (chartModuleLoader.url == ti1.text) {
28.
29. // If they are the same, call loadModule.
30.
31. chartModuleLoader.loadModule();
32.
33. } else {
34.
35. // If they are not the same, then change the url,
36.
37. // which triggers a call to the loadModule() method.
38.
39. chartModuleLoader.url = ti1.text;
40.
41. }
42.
43. }
44.
45. public function removeModule():void {
46.
47. chartModuleLoader.unloadModule();
48.
49. }
50.
51. ]]>
52.
53. </mx:Script>
54.
55. <mx:Panel title="Module Example"
56.
57. height="90%"
58.
59. width="90%"
60.
61. paddingTop="10"
62.
63. paddingLeft="10"
64.
65. paddingRight="10"
66.
67. paddingBottom="10"
68.
69. >
70.
71. <mx:HBox>
72.
73. <mx:Label text="URL:"/>
74.
75. <mx:TextInput width="200" id="ti1" text="ColumnChartModule.swf"/
76.
77. >
78.
79. <mx:Button label="Load" click="createModule()"/>
80.
81. <mx:Button label="Unload" click="removeModule()"/>
82.
83. </mx:HBox>
84.
85. <mx:ModuleLoader id="chartModuleLoader" error="errorHandler(event)"/
86.
87. >
88.
89. </mx:Panel>
90.
91. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import mx.modules.*;
import mx.controls.Alert;
private function errorHandler(e:ModuleEvent):void {
Alert.show("There was an error loading the module." +
" Please contact the Help Desk.");
trace(e.errorText);
}
public function createModule():void {
if (chartModuleLoader.url == ti1.text) {
// If they are the same, call loadModule.
chartModuleLoader.loadModule();
} else {
// If they are not the same, then change the url,
// which triggers a call to the loadModule() method.
chartModuleLoader.url = ti1.text;
}
}
public function removeModule():void {
chartModuleLoader.unloadModule();
}
]]>
</mx:Script>
<mx:Panel title="Module Example"
height="90%"
width="90%"
paddingTop="10"
paddingLeft="10"
paddingRight="10"
paddingBottom="10"
>
<mx:HBox>
<mx:Label text="URL:"/>
<mx:TextInput width="200" id="ti1" text="ColumnChartModule.swf"/
>
<mx:Button label="Load" click="createModule()"/>
<mx:Button label="Unload" click="removeModule()"/>
</mx:HBox>
<mx:ModuleLoader id="chartModuleLoader" error="errorHandler(event)"/
>
</mx:Panel>
</mx:Application>
应用progress事件
使用ModuleLoader的progress事件可以让你跟踪模块实例加载的进度信息。参考示例,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/SimpleProgressEventHandler.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. import mx.events.ModuleEvent;
12.
13. import flash.events.ProgressEvent;
14.
15. import mx.modules.*;
16.
17. [Bindable]
18.
19. public var progBar:String = "";
20.
21. [Bindable]
22.
23. public var progMessage:String = "";
24.
25. private function progressEventHandler(e:ProgressEvent):void {
26.
27. progBar += ".";
28.
29. progMessage =
30.
31. "Module " +
32.
33. Math.round((e.bytesLoaded/e.bytesTotal) * 100) +
34.
35. "% loaded";
36.
37. }
38.
39. public function createModule():void {
40.
41. chartModuleLoader.loadModule();
42.
43. }
44.
45. public function removeModule():void {
46.
47. chartModuleLoader.unloadModule();
48.
49. progBar = "";
50.
51. progMessage = "";
52.
53. }
54.
55. ]]>
56.
57. </mx:Script>
58.
59. <mx:Panel title="Module Example"
60.
61. height="90%"
62.
63. width="90%"
64.
65. paddingTop="10"
66.
67. paddingLeft="10"
68.
69. paddingRight="10"
70.
71. paddingBottom="10"
72.
73. >
74.
75. <mx:HBox>
76.
77. <mx:Label id="l2" text="{progMessage}"/>
78.
79. <mx:Label id="l1" text="{progBar}"/>
80.
81. </mx:HBox>
82.
83. <mx:Button label="Load" click="createModule()"/>
84.
85. <mx:Button label="Unload" click="removeModule()"/>
86.
87. <mx:ModuleLoader
88.
89. id="chartModuleLoader"
90.
91. url="ColumnChartModule.swf"
92.
93. progress="progressEventHandler(event)"
94.
95. />
96.
97. </mx:Panel>
98.
99. </mx:Application>
<?xml version="1.0"?>
<!-- modules/SimpleProgressEventHandler.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import flash.events.ProgressEvent;
import mx.modules.*;
[Bindable]
public var progBar:String = "";
[Bindable]
public var progMessage:String = "";
private function progressEventHandler(e:ProgressEvent):void {
progBar += ".";
progMessage =
"Module " +
Math.round((e.bytesLoaded/e.bytesTotal) * 100) +
"% loaded";
}
public function createModule():void {
chartModuleLoader.loadModule();
}
public function removeModule():void {
chartModuleLoader.unloadModule();
progBar = "";
progMessage = "";
}
]]>
</mx:Script>
<mx:Panel title="Module Example"
height="90%"
width="90%"
paddingTop="10"
paddingLeft="10"
paddingRight="10"
paddingBottom="10"
>
<mx:HBox>
<mx:Label id="l2" text="{progMessage}"/>
<mx:Label id="l1" text="{progBar}"/>
</mx:HBox>
<mx:Button label="Load" click="createModule()"/>
<mx:Button label="Unload" click="removeModule()"/>
<mx:ModuleLoader
id="chartModuleLoader"
url="ColumnChartModule.swf"
progress="progressEventHandler(event)"
/>
</mx:Panel>
</mx:Application>
怎样共享和传输Module模块间数据
Module模块是一个容器,每个独立的模块对象都相当于一个自定义组件。有以下几种方式可以实现模块-模块、模块-主应用、主应用-模块、模块-一般自定义组件间的数据传输和通信。
利用ModuleLoader的child、ModuleManager的factory、以及Application的parentApplication属性存取Module模块和主应用文件对象索引。
因为Module模块在ModuleLoader中通常用url属性来指定,所以我们可以通过在url上面拼GET参数,然后在Module模块中解析这些拼上去的参数的方式来传输数据。
通过ActionScript接口方式。你可以定义一个ActionScript接口,这个接口定义了一系列方法和属性。Module模块和主应用Application都可以访问这些属性。从而实现数据共享。
在主应用Application中访问Module模块对象
我们可以在主应用Application中访问子Module模块对象中定义的方法和属性。下面示例展示了通过ModuleLoader的child属性拿到子Module模块对象索引,然后调用其定义的公共方法getTitle().
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/ParentApplication.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script><![CDATA[
8.
9. [Bindable]
10.
11. private var s:String;
12.
13. private function getTitle():void {
14.
15. s = (m1.child as ChildModule1).getModTitle();
16.
17. }
18.
19. ]]></mx:Script>
20.
21. <mx:Label id="l1" text="{s}"/>
22.
23. <mx:ModuleLoader url="ChildModule1.swf" id="m1" ready="getTitle()"/>
24.
25. </mx:Application>
<?xml version="1.0"?>
<!-- modules/ParentApplication.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
[Bindable]
private var s:String;
private function getTitle():void {
s = (m1.child as ChildModule1).getModTitle();
}
]]></mx:Script>
<mx:Label id="l1" text="{s}"/>
<mx:ModuleLoader url="ChildModule1.swf" id="m1" ready="getTitle()"/>
</mx:Application>
Module模块文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/ChildModule1.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%">
8.
9. <mx:Script><![CDATA[
10.
11. // Defines the method that the application calls.
12.
13. public function getModTitle():String {
14.
15. return "Child Module 1";
16.
17. }
18.
19. ]]></mx:Script>
20.
21. </mx:Module>
<?xml version="1.0"?>
<!-- modules/ChildModule1.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%">
<mx:Script><![CDATA[
// Defines the method that the application calls.
public function getModTitle():String {
return "Child Module 1";
}
]]></mx:Script>
</mx:Module>
通过这种方式得到子Module模块的索引导致了主应用Application和Module模块之间的紧耦合,不利于模块逻辑的重用。同样地,利用ModuleManager的factory属性也可以拿到当前ModuleLoader的子模块对象的索引。参考以下示例,模块文件为,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/mxmlmodules/SimpleModule.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. public function computeAnswer(a:Number, b:Number):Number {
12.
13. return a + b;
14.
15. }
16.
17. ]]>
18.
19. </mx:Script>
20.
21. </mx:Module>
<?xml version="1.0"?>
<!-- modules/mxmlmodules/SimpleModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
public function computeAnswer(a:Number, b:Number):Number {
return a + b;
}
]]>
</mx:Script>
</mx:Module>
主应用Application为,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/mxmlmodules/SimpleMXMLApp.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
6.
7. creationComplete="initApp()">
8.
9. <mx:Script>
10.
11. <![CDATA[
12.
13. import mx.modules.IModuleInfo;
14.
15. import mx.modules.ModuleManager;
16.
17. public var assetModule:IModuleInfo;
18.
19. public var sm:Object;
20.
21. [Bindable]
22.
23. public var answer:Number = 0;
24.
25. public function initApp():void {
26.
27. // Get the IModuleInfo interface for the specified URL.
28.
29. assetModule = ModuleManager.getModule("SimpleModule.swf");
30.
31. assetModule.addEventListener("ready", getModuleInstance);
32.
33. assetModule.load();
34.
35. }
36.
37. public function getModuleInstance(e:Event):void {
38.
39. // Get an instance of the module.
40.
41. sm = assetModule.factory.create() as SimpleModule;
42.
43. }
44.
45. public function addNumbers():void {
46.
47. var a:Number = Number(ti1.text);
48.
49. var b:Number = Number(ti2.text);
50.
51. // Call a method on the module.
52.
53. answer = sm.computeAnswer(a, b).toString();
54.
55. }
56.
57. ]]>
58.
59. </mx:Script>
60.
61. <mx:Form>
62.
63. <mx:FormHeading label="Enter values to sum."/>
64.
65. <mx:FormItem label="First Number">
66.
67. <mx:TextInput id="ti1" width="50"/>
68.
69. </mx:FormItem>
70.
71. <mx:FormItem label="Second Number">
72.
73. <mx:TextInput id="ti2" width="50"/>
74.
75. </mx:FormItem>
76.
77. <mx:FormItem label="Result">
78.
79. <mx:Label id="ti3" width="100" text="{answer}"/>
80.
81. </mx:FormItem>
82.
83. <mx:Button id="b1" label="Compute" click="addNumbers()"/>
84.
85. </mx:Form>
86.
87. </mx:Application>
<?xml version="1.0"?>
<!-- modules/mxmlmodules/SimpleMXMLApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.modules.IModuleInfo;
import mx.modules.ModuleManager;
public var assetModule:IModuleInfo;
public var sm:Object;
[Bindable]
public var answer:Number = 0;
public function initApp():void {
// Get the IModuleInfo interface for the specified URL.
assetModule = ModuleManager.getModule("SimpleModule.swf");
assetModule.addEventListener("ready", getModuleInstance);
assetModule.load();
}
public function getModuleInstance(e:Event):void {
// Get an instance of the module.
sm = assetModule.factory.create() as SimpleModule;
}
public function addNumbers():void {
var a:Number = Number(ti1.text);
var b:Number = Number(ti2.text);
// Call a method on the module.
answer = sm.computeAnswer(a, b).toString();
}
]]>
</mx:Script>
<mx:Form>
<mx:FormHeading label="Enter values to sum."/>
<mx:FormItem label="First Number">
<mx:TextInput id="ti1" width="50"/>
</mx:FormItem>
<mx:FormItem label="Second Number">
<mx:TextInput id="ti2" width="50"/>
</mx:FormItem>
<mx:FormItem label="Result">
<mx:Label id="ti3" width="100" text="{answer}"/>
</mx:FormItem>
<mx:Button id="b1" label="Compute" click="addNumbers()"/>
</mx:Form>
</mx:Application>
在Module模块对象中存取主应用Application
Module模块可以通过其parentApplication属性拿到主应用Application对象的索引,通过这个索引可以访问主应用对象的公共方法和属性。参考以下示例,Module模块文件为,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/ChartChildModule.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%" creationComplete="getDataFromParent()">
8.
9. <mx:Script><![CDATA[
10.
11. import mx.collections.ArrayCollection;
12.
13. [Bindable]
14.
15. private var expenses:ArrayCollection;
16.
17. // Access properties of the parent application.
18.
19. private function getDataFromParent():void {
20.
21. expenses = parentApplication.expenses;
22.
23. }
24.
25. ]]></mx:Script>
26.
27. <mx:ColumnChart id="myChart" dataProvider="{expenses}">
28.
29. <mx:horizontalAxis>
30.
31. <mx:CategoryAxis
32.
33. dataProvider="{expenses}"
34.
35. categoryField="Month"
36.
37. />
38.
39. </mx:horizontalAxis>
40.
41. <mx:series>
42.
43. <mx:ColumnSeries
44.
45. xField="Month"
46.
47. yField="Profit"
48.
49. displayName="Profit"
50.
51. />
52.
53. <mx:ColumnSeries
54.
55. xField="Month"
56.
57. yField="Expenses"
58.
59. displayName="Expenses"
60.
61. />
62.
63. </mx:series>
64.
65. </mx:ColumnChart>
66.
67. <mx:Legend dataProvider="{myChart}"/>
68.
69. <mx:Button id="b1" click="expenses = parentApplication.getNewData();"
70.
71. label="Get New Data"/>
72.
73. </mx:Module>
<?xml version="1.0"?>
<!-- modules/ChartChildModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%" creationComplete="getDataFromParent()">
<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var expenses:ArrayCollection;
// Access properties of the parent application.
private function getDataFromParent():void {
expenses = parentApplication.expenses;
}
]]></mx:Script>
<mx:ColumnChart id="myChart" dataProvider="{expenses}">
<mx:horizontalAxis>
<mx:CategoryAxis
dataProvider="{expenses}"
categoryField="Month"
/>
</mx:horizontalAxis>
<mx:series>
<mx:ColumnSeries
xField="Month"
yField="Profit"
displayName="Profit"
/>
<mx:ColumnSeries
xField="Month"
yField="Expenses"
displayName="Expenses"
/>
</mx:series>
</mx:ColumnChart>
<mx:Legend dataProvider="{myChart}"/>
<mx:Button id="b1" click="expenses = parentApplication.getNewData();"
label="Get New Data"/>
</mx:Module>
主应用文件为,
<?xml version="1.0"?>
Java代码
1. <!-- modules/ChartChildModuleLoader.mxml -->
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Script><![CDATA[
6.
7. import mx.collections.ArrayCollection;
8.
9. [Bindable]
10.
11. public var expenses:ArrayCollection = new ArrayCollection([
12.
13. {Month:"Jan", Profit:2000, Expenses:1500},
14.
15. {Month:"Feb", Profit:1000, Expenses:200},
16.
17. {Month:"Mar", Profit:1500, Expenses:500}
18.
19. ]);
20.
21. public function getNewData():ArrayCollection {
22.
23. return new ArrayCollection([
24.
25. {Month:"Apr", Profit:1000, Expenses:1100},
26.
27. {Month:"May", Profit:1300, Expenses:500},
28.
29. {Month:"Jun", Profit:1200, Expenses:600}
30.
31. ]);
32.
33. }
34.
35. ]]></mx:Script>
36.
37. <mx:ModuleLoader url="ChartChildModule.swf" id="m1"/>
38.
39. </mx:Application>
<!-- modules/ChartChildModuleLoader.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var expenses:ArrayCollection = new ArrayCollection([
{Month:"Jan", Profit:2000, Expenses:1500},
{Month:"Feb", Profit:1000, Expenses:200},
{Month:"Mar", Profit:1500, Expenses:500}
]);
public function getNewData():ArrayCollection {
return new ArrayCollection([
{Month:"Apr", Profit:1000, Expenses:1100},
{Month:"May", Profit:1300, Expenses:500},
{Month:"Jun", Profit:1200, Expenses:600}
]);
}
]]></mx:Script>
<mx:ModuleLoader url="ChartChildModule.swf" id="m1"/>
</mx:Application>
不过这种方式导致的一个缺点是,自定义的Module模块文件的可移植特性将大打折扣。
在一个Module模块对象中存取另一个Module模块对象
同样地,我们也可以利用ModuleLoader的child属性,在一个Module模块对象中拿到另一个Module模块对象的索引,通过这个索引访问当前Module模块对象的公共方法和属性。参考以下示例,主应用文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/TitleModuleLoader.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script><![CDATA[
8.
9. ]]></mx:Script>
10.
11. <mx:ModuleLoader url="InterModule1.swf" id="m1"/>
12.
13. <mx:ModuleLoader url="InterModule2.swf" id="m2"/>
14.
15. </mx:Application>
<?xml version="1.0"?>
<!-- modules/TitleModuleLoader.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
]]></mx:Script>
<mx:ModuleLoader url="InterModule1.swf" id="m1"/>
<mx:ModuleLoader url="InterModule2.swf" id="m2"/>
</mx:Application>
Module1文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/InterModule1.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%">
8.
9. <mx:Script><![CDATA[
10.
11. // Defines the method that the other module calls.
12.
13. public function getNewTitle():String {
14.
15. return "New Module Title";
16.
17. }
18.
19. ]]></mx:Script>
20.
21. </mx:Module>
<?xml version="1.0"?>
<!-- modules/InterModule1.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%">
<mx:Script><![CDATA[
// Defines the method that the other module calls.
public function getNewTitle():String {
return "New Module Title";
}
]]></mx:Script>
</mx:Module>
Module2文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/InterModule2.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%">
8.
9. <mx:Script><![CDATA[
10.
11. [Bindable]
12.
13. private var title:String;
14.
15. // Call method of another module.
16.
17. private function changeTitle():void {
18.
19. title = parentApplication.m1.child.getNewTitle();
20.
21. }
22.
23. ]]></mx:Script>
24.
25. <mx:HBox>
26.
27. <mx:Label id="l1" text="Title: "/>
28.
29. <mx:Label id="myTitle" text="{title}"/>
30.
31. </mx:HBox>
32.
33. <mx:Button id="b1" label="Change Title" click="changeTitle()"/>
34.
35. </mx:Module>
<?xml version="1.0"?>
<!-- modules/InterModule2.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%">
<mx:Script><![CDATA[
[Bindable]
private var title:String;
// Call method of another module.
private function changeTitle():void {
title = parentApplication.m1.child.getNewTitle();
}
]]></mx:Script>
<mx:HBox>
<mx:Label id="l1" text="Title: "/>
<mx:Label id="myTitle" text="{title}"/>
</mx:HBox>
<mx:Button id="b1" label="Change Title" click="changeTitle()"/>
</mx:Module>
通过拼ModuleLoader的url参数方式实现数据传输
在url上面拼GET参数基本上是这种格式,url=module1.swf?param1=value1¶m2=value2
比如在主应用文件Application中拼一系列GET参数到ModuleLoader的url属性上,在Module模块文件中解析并处理这些参数,参考以下示例,主应用文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/QueryStringApp.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="500"
6.
7. width="400">
8.
9. <mx:Script><![CDATA[
10.
11. public function initModule():void {
12.
13. // Build query string so that it looks something like this:
14.
15. // "QueryStringModule.swf?firstName=Nick&lastName=Danger"
16.
17. var s:String = "QueryStringModule.swf?" + "firstName=" +
18.
19. ti1.text + "&lastName=" + ti2.text;
20.
21. // Changing the url property of the ModuleLoader causes
22.
23. // the ModuleLoader to load a new module.
24.
25. m1.url = s;
26.
27. }
28.
29. ]]></mx:Script>
30.
31. <mx:Form>
32.
33. <mx:FormItem id="fi1" label="First Name:">
34.
35. <mx:TextInput id="ti1"/>
36.
37. </mx:FormItem>
38.
39. <mx:FormItem id="fi2" label="Last Name:">
40.
41. <mx:TextInput id="ti2"/>
42.
43. </mx:FormItem>
44.
45. </mx:Form>
46.
47. <mx:ModuleLoader id="m1"/>
48.
49. <mx:Button id="b1" label="Submit" click="initModule()"/>
50.
51. </mx:Application>
<?xml version="1.0"?>
<!-- modules/QueryStringApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="500"
width="400">
<mx:Script><![CDATA[
public function initModule():void {
// Build query string so that it looks something like this:
// "QueryStringModule.swf?firstName=Nick&lastName=Danger"
var s:String = "QueryStringModule.swf?" + "firstName=" +
ti1.text + "&lastName=" + ti2.text;
// Changing the url property of the ModuleLoader causes
// the ModuleLoader to load a new module.
m1.url = s;
}
]]></mx:Script>
<mx:Form>
<mx:FormItem id="fi1" label="First Name:">
<mx:TextInput id="ti1"/>
</mx:FormItem>
<mx:FormItem id="fi2" label="Last Name:">
<mx:TextInput id="ti2"/>
</mx:FormItem>
</mx:Form>
<mx:ModuleLoader id="m1"/>
<mx:Button id="b1" label="Submit" click="initModule()"/>
</mx:Application>
模块文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/QueryStringModule.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"
6.
7. creationComplete="parseString()">
8.
9. <mx:Script>
10.
11. <![CDATA[
12.
13. import mx.utils.*;
14.
15. [Bindable]
16.
17. private var salutation:String;
18.
19. public var o:Object = {};
20.
21. public function parseString():void {
22.
23. try {
24.
25. // Remove everything before the question mark, including
26.
27. // the question mark.
28.
29. var myPattern:RegExp = /.*\?/;
30.
31. var s:String = this.loaderInfo.url.toString();
32.
33. s = s.replace(myPattern, "");
34.
35. // Create an Array of name=value Strings.
36.
37. var params:Array = s.split("&");
38.
39. // Print the params that are in the Array.
40.
41. var keyStr:String;
42.
43. var valueStr:String;
44.
45. var paramObj:Object = params;
46.
47. for (keyStr in paramObj) {
48.
49. valueStr = String(paramObj[keyStr]);
50.
51. ta1.text += keyStr + ":" + valueStr + "\n";
52.
53. }
54.
55. // Set the values of the salutation.
56.
57. for (var i:int = 0; i < params.length; i++) {
58.
59. var tempA:Array = params[i].split("=");
60.
61. if (tempA[0] == "firstName") {
62.
63. o.firstName = tempA[1];
64.
65. }
66.
67. if (tempA[0] == "lastName") {
68.
69. o.lastName = tempA[1];
70.
71. }
72.
73. }
74.
75. if (StringUtil.trim(o.firstName) != "" &&
76.
77. StringUtil.trim(o.lastName) != "") {
78.
79. salutation = "Welcome " +
80.
81. o.firstName + " " + o.lastName + "!";
82.
83. } else {
84.
85. salutation = "Full name not entered."
86.
87. }
88.
89. } catch (e:Error) {
90.
91. trace(e);
92.
93. }
94.
95. // Show some of the information available through loaderInfo:
96.
97. trace("AS version: " + this.loaderInfo.actionScriptVersion);
98.
99. trace("App height: " + this.loaderInfo.height);
100.
101. trace("App width: " + this.loaderInfo.width);
102.
103. trace("App bytes: " + this.loaderInfo.bytesTotal);
104.
105. }
106.
107. ]]>
108.
109. </mx:Script>
110.
111. <mx:Label text="{salutation}"/>
112.
113. <mx:TextArea height="100" width="300" id="ta1"/>
114.
115. </mx:Module>
<?xml version="1.0"?>
<!-- modules/QueryStringModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="parseString()">
<mx:Script>
<![CDATA[
import mx.utils.*;
[Bindable]
private var salutation:String;
public var o:Object = {};
public function parseString():void {
try {
// Remove everything before the question mark, including
// the question mark.
var myPattern:RegExp = /.*\?/;
var s:String = this.loaderInfo.url.toString();
s = s.replace(myPattern, "");
// Create an Array of name=value Strings.
var params:Array = s.split("&");
// Print the params that are in the Array.
var keyStr:String;
var valueStr:String;
var paramObj:Object = params;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]);
ta1.text += keyStr + ":" + valueStr + "\n";
}
// Set the values of the salutation.
for (var i:int = 0; i < params.length; i++) {
var tempA:Array = params[i].split("=");
if (tempA[0] == "firstName") {
o.firstName = tempA[1];
}
if (tempA[0] == "lastName") {
o.lastName = tempA[1];
}
}
if (StringUtil.trim(o.firstName) != "" &&
StringUtil.trim(o.lastName) != "") {
salutation = "Welcome " +
o.firstName + " " + o.lastName + "!";
} else {
salutation = "Full name not entered."
}
} catch (e:Error) {
trace(e);
}
// Show some of the information available through loaderInfo:
trace("AS version: " + this.loaderInfo.actionScriptVersion);
trace("App height: " + this.loaderInfo.height);
trace("App width: " + this.loaderInfo.width);
trace("App bytes: " + this.loaderInfo.bytesTotal);
}
]]>
</mx:Script>
<mx:Label text="{salutation}"/>
<mx:TextArea height="100" width="300" id="ta1"/>
</mx:Module>
利用ActionScript接口实现Module模块间的数据通信
在面向对象的编程中,我们讲要面向接口编程。面向接口的编程方式从一定程度上解决了相互关联的模块间的紧密耦合问题。以上提到的所有数据传输和共享方式都在不同程度上导致了模块间的紧耦合。不过,Flex提供了一种利用标准的ActionScript接口实现Module模块间数据通信的方式。具体地说,对于Module模块对象和主应用Application对象间的通信,我们可以定义一个ActionScript接口,Module模块对象实现了这个接口中定义的方法和属性,那么主应用Application就可以访问这个接口中定义的属性和方法。接口中定义了Module模块对象和主应用 Application需要共享的数据和方法,是两者间共同的一个契约,同时也实现了接口和实现的分离,达到了松耦合的目的。参考以下示例,主应用 Application,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/interfaceexample/Main.mxml -->
4.
5. <mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. import mx.events.ModuleEvent;
12.
13. import mx.modules.ModuleManager;
14.
15. [Bindable]
16.
17. public var selectedItem:Object;
18.
19. [Bindable]
20.
21. public var currentModuleName:String;
22.
23. private function applyModuleSettings(e:Event):void {
24.
25. // Cast the ModuleLoader's child to the interface.
26.
27. // This child is an instance of the module.
28.
29. // You can now call methods on that instance.
30.
31. var ichild:* = mod.child as IModuleInterface;
32.
33. if (mod.child != null) {
34.
35. // Call setters in the module to adjust its
36.
37. // appearance when it loads.
38.
39. ichild.setAdjusterID(myId.text);
40.
41. ichild.setBackgroundColor(myColor.selectedColor);
42.
43. } else {
44.
45. trace("Uh oh. The mod.child property is null");
46.
47. }
48.
49. // Set the value of a local variable by calling a method
50.
51. // on the interface.
52.
53. currentModuleName = ichild.getModuleName();
54.
55. }
56.
57. private function reloadModule():void {
58.
59. mod.unloadModule();
60.
61. mod.loadModule();
62.
63. }
64.
65. ]]>
66.
67. </mx:Script>
68.
69. <mx:Form>
70.
71. <mx:FormItem label="Current Module:">
72.
73. <mx:Label id="l1" text="{currentModuleName}"/>
74.
75. </mx:FormItem>
76.
77. <mx:FormItem label="Adjuster ID:">
78.
79. <mx:TextInput id="myId" text="Enter your ID"/>
80.
81. </mx:FormItem>
82.
83. <mx:FormItem label="Background Color:">
84.
85. <mx:ColorPicker id="myColor"
86.
87. selectedColor="0xFFFFFF"
88.
89. change="reloadModule()"
90.
91. />
92.
93. </mx:FormItem>
94.
95. </mx:Form>
96.
97. <mx:Label text="Long Shot Insurance" fontSize="24"/>
98.
99. <mx:ComboBox
Module加载方式概览
在理解这种加载方式之前,我们应该首先知道什么是Module模块。Module 实际上是一个预编译的SWF文件。虽然是SWF格式的文件,但是这个文件不能独立运行,并且只能被ModuleLoader加载后才能显示。逻辑上它是一个容器,可以像一般的容器一样包含别的容器,组件,甚至是别的Module模块。根据需要,预编译的Module模块可以被应用加载和卸载。不同的应用可以共享这些Module模块。Flex应用可以被分割为若干个预编译的Module模块,可以在需要时分别加载这些模块,避免在系统初始化时加载全部子容器。采用这种方式的Flex应用从设计上分隔了逻辑相对独立的模块,减少了系统初始化时的加载时间。
Module的创建、编译、加载和卸载方法
我们有两种方式创建一个Module模块,一是利用MXML标签<mx:Module>创建Module类;令一种方式采用 ModuleManager类在ActionScript中创建Module模块类。模块类创建后将被编译成SWF文件。如前面章节所述我们可以利用 mxmlc编译器手动编译或者在Flex Builder3集成开发环境提供的工具自动编译Module类为SWF文件。这里介绍采用Flex Builder3集成开发环境提供的方式创建一个Module类。
在FlexBuilder3中创建基于MXML标签的Module模块
创建基于MXML标签的Module模块,需要扩展mx.modules.Module.
1. 第一步,在Flex Builder3集成开发环境中创建一个Flex项目Moduler。
2. 第二步,选择FileàNewàMXML Module
3. 第三步,输入Module文件名字为MXMLDemoModule,设置Module的高度和宽度,选择Module容器的布局方式为absolute.,选择默认的预编译后优化的SWF选项,单击Finish按钮。
4. Module文件被创建,编辑Module文件,在此例中我们可以假定是一个登陆Module界面,
其源代码为,
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
4.
5. <mx:Panel width="300" height="220" layout="absolute" horizontalCenter="0"
6.
7. verticalCenter="0" backgroundAlpha="0.5">
8.
9. <mx:Form horizontalCenter="0" verticalCenter="0" paddingBottom="0" paddingLeft="0" paddingRight="0"
10.
11. paddingTop="0">
12.
13. <mx:FormItem label="Account" indicatorGap="5">
14.
15. <mx:TextInput borderStyle="solid" id="account" text="admin"/>
16.
17. </mx:FormItem>
18.
19. <mx:FormItem label="Password" indicatorGap="5">
20.
21. <mx:TextInput borderStyle="solid" id="password" text="admin" displayAsPassword="true"/>
22.
23. </mx:FormItem>
24.
25. <mx:FormItem direction="horizontal" >
26.
27. <mx:Button id="logined" label="Login" styleName="blueButton"/>
28.
29. <mx:Button id="reset" label="Reset" styleName="greenButton"/>
30.
31. </mx:FormItem>
32.
33. </mx:Form>
34.
35. </mx:Panel>
36.
37. <mx:Label text="CopyRight 2008 demo Solutions" horizontalCenter="0" verticalCenter="252.5"/>
38.
39. </mx:Module>
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
<mx:Panel width="300" height="220" layout="absolute" horizontalCenter="0"
verticalCenter="0" backgroundAlpha="0.5">
<mx:Form horizontalCenter="0" verticalCenter="0" paddingBottom="0" paddingLeft="0" paddingRight="0"
paddingTop="0">
<mx:FormItem label="Account" indicatorGap="5">
<mx:TextInput borderStyle="solid" id="account" text="admin"/>
</mx:FormItem>
<mx:FormItem label="Password" indicatorGap="5">
<mx:TextInput borderStyle="solid" id="password" text="admin" displayAsPassword="true"/>
</mx:FormItem>
<mx:FormItem direction="horizontal" >
<mx:Button id="logined" label="Login" styleName="blueButton"/>
<mx:Button id="reset" label="Reset" styleName="greenButton"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
<mx:Label text="CopyRight 2008 demo Solutions" horizontalCenter="0" verticalCenter="252.5"/>
</mx:Module>
集成登陆Module模块到主应用文件ModulerApp.mxml,
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
4.
5. layout="absolute">
6.
7. <mx:ModuleLoader url="MXMLDemoModule.swf"/>
8.
9. </mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:ModuleLoader url="MXMLDemoModule.swf"/>
</mx:Application>
创建基于ActionScript的Module模块
在ActionScript中创建的Module模块类大都继承了mx.modules.Module或者 mx.modules.ModuleBase基础类。我们都知道,MXML标签<mx:Module>实际上是 mx.modules.Module类的另一种表现方式,这也就不难理解无论在MXML标签中还是在ActionScript中创建Module模块其实都是相通的。自定义Module类继承mx.modules.Module和继承mx.modules.ModuleBase两个基类的不同是,继承自前者的或者基于<mx:Module>标签的自定义Module组件将被加入到框架可视化显示列表,后者则没有。参考示例
Java代码
1. package {
2.
3. import mx.modules.ModuleBase;
4.
5. public class MyModule extends ModuleBase {
6.
7. public function MyModule() {
8.
9. trace("MyModule was created!");
10.
11. }
12.
13. }
14.
15. }
package {
import mx.modules.ModuleBase;
public class MyModule extends ModuleBase {
public function MyModule() {
trace("MyModule was created!");
}
}
}
编译Module模块
前面已经提及我们有两种方式来编译Module模块文件。像编译主应用文件一样,一种是在命令行手动编译,最简单的情形 可以利用这个命令
Mxmlc MyModule.mxml
另一种是在Flex Builder3中提供的自动编译工具编译。编译的结果是一个可以被装载的SWF文件,与编译后的SWF主应用文件最大的不同就是,模块SWF文件不能独立运行,只能在被装载后才能够和其宿主共同运行。
怎样装载和卸载Module模块
总体来说,有几种方式可以实现Module模块的装载和卸载。
利用ModuleLoader:ModuleLoader类提供了一系列高层处理Module的编程接口。
利用ModuleManager: ModuleManager类提供了低层次的处理Module的装载卸载以及事件响应等的变成接口。
下面将详细地讲解这两种处理方式。
我们可以利用ModuleLoader类在主应用文件或者别的Module模块中加载任意预编译的Module对象。最简单的方式是采用 ModuleLoader类的标签形式<mx:ModuleLoader>在主应用的MXML文件中显式地加载Module,然后只需要设置这个标签的url属性为预编译Module的SWF文件位置,可以是相对路径,也可以是绝对路径。参考实例,
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
4.
5. layout="absolute">
6.
7. <mx:ModuleLoader url="MXMLDemoModule.swf"/>
8.
9. </mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:ModuleLoader url="MXMLDemoModule.swf"/>
</mx:Application>
编程时我们可以根据需要替换此url所指向的模块位置,实现不同模块的更迭显示。当ModuleLoader类初始化时或者每次更改这个url属性值时,这个ModuleLoader类的loadModule()方法被触发。如果设定url属性为空字符串,ModuleLoader卸载当前加载的 Module模块。
ModuleLoader其实是一种特殊的导航式容器。和一般导航式容器如ViewStack不同的是,ModuleLoader不必在初始化时携带加载所有的孩子组件。了解这一点,我们可以猜想到,在一个Flex主应用中可以包含甚至嵌套包含多个ModuleLoader实例,以下示例展示在一个ViewStack容器中包含多个ModuleLoader的示例。
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Panel
6.
7. title="Multiple Modules Demo "
8.
9. height="100%"
10.
11. width="100%"
12.
13. paddingTop="0"
14.
15. paddingLeft="0"
16.
17. paddingRight="0"
18.
19. paddingBottom="0"
20.
21. >
22.
23. <mx:VBox id="v1" label="Module1">
24.
25. <mx:Label id="l1" text="Module1.swf"/>
26.
27. <mx:ModuleLoader url="Module1.swf"/>
28.
29. </mx:VBox>
30.
31.
32.
33. <mx:VBox id="v2" label="Module2">
34.
35. <mx:Label id="l2" text="Module2.swf"/>
36.
37. <mx:ModuleLoader url="Module2.swf"/>
38.
39. </mx:VBox>
40.
41.
42.
43. <mx:VBox id="v3" label="Module3">
44.
45. <mx:Label id="l3" text="Module3.swf"/>
46.
47. <mx:ModuleLoader url="Module3.swf"/>
48.
49. </mx:VBox>
50.
51. </mx:TabNavigator>
52.
53. </mx:Panel>
54.
55. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Panel
title="Multiple Modules Demo "
height="100%"
width="100%"
paddingTop="0"
paddingLeft="0"
paddingRight="0"
paddingBottom="0"
>
<mx:VBox id="v1" label="Module1">
<mx:Label id="l1" text="Module1.swf"/>
<mx:ModuleLoader url="Module1.swf"/>
</mx:VBox>
<mx:VBox id="v2" label="Module2">
<mx:Label id="l2" text="Module2.swf"/>
<mx:ModuleLoader url="Module2.swf"/>
</mx:VBox>
<mx:VBox id="v3" label="Module3">
<mx:Label id="l3" text="Module3.swf"/>
<mx:ModuleLoader url="Module3.swf"/>
</mx:VBox>
</mx:TabNavigator>
</mx:Panel>
</mx:Application>
同时我们可以利用ModuleLoader类提供的loadModule()和unloadModule()方法动态地指定被加载的Module 模块。这两个方法没有输入形参,调用时分别加载和卸载当前ModuleLoader实例url属性所指向的Module模块。下面示例展示了在 TabNavigator容器中,按钮点击后分别加载卸载不同的Module模块
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Script>
6.
7. <![CDATA[
8.
9. import mx.modules.*;
10.
11. public function moduleLoader(m:ModuleLoader, s:String):void {
12.
13. if (!m.url) {
14.
15. m.url = s;
16.
17. return;
18.
19. }
20.
21. m.loadModule();
22.
23. }
24.
25. public function moduleUnloader(m:ModuleLoader):void {
26.
27. m.unloadModule();
28.
29. }
30.
31. ]]>
32.
33. </mx:Script>
34.
35. <mx:Panel title="Dynamically load/unload multiple Modules"
36.
37. height="100%"
38.
39. width="100%"
40.
41. paddingTop="0"
42.
43. paddingLeft="0"
44.
45. paddingRight="0"
46.
47. paddingBottom="0"
48.
49. >
50.
51. <mx:TabNavigator id="navigator"
52.
53. width="100%"
54.
55. height="100%"
56.
57. creationPolicy="all"
58.
59. >
60.
61. <mx:VBox id="vb1" label="Module1">
62.
63. <mx:Button label="Load" click="moduleLoader(moduleLoader, l1.text)" />
64.
65. <mx:Button label="Unload" click="moduleUnloader(moduleLoader)" />
66.
67. <mx:Label id="l1" text="Module1.swf"/>
68.
69. <mx:ModuleLoader id="moduleLoader"/>
70.
71. </mx:VBox>
72.
73.
74.
75. <mx:VBox id="vb2" label="Module2">
76.
77. <mx:Button label="Load" click="moduleLoader(moduleLoader2, l2.text)"/>
78.
79. <mx:Button label="Unload" click="removeModule(moduleLoader2)" />
80.
81. <mx:Label id="l2" text="Module2.swf"/>
82.
83. <mx:ModuleLoader id="moduleLoader2"/>
84.
85. </mx:VBox>
86.
87.
88.
89. <mx:VBox id="vb3" label="Module3">
90.
91. <mx:Button label="Load" click="moduleLoader(moduleLoader3, l3.text)"/>
92.
93. <mx:Button label="Unload" click="removeModule(moduleLoader3)" />
94.
95. <mx:Label id="l3" text="Module3.swf"/>
96.
97. <mx:ModuleLoader id="moduleLoader3"/>
98.
99. </mx:VBox>
100.
101.
102.
103. </mx:TabNavigator>
104.
105. </mx:Panel>
106.
107. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.modules.*;
public function moduleLoader(m:ModuleLoader, s:String):void {
if (!m.url) {
m.url = s;
return;
}
m.loadModule();
}
public function moduleUnloader(m:ModuleLoader):void {
m.unloadModule();
}
]]>
</mx:Script>
<mx:Panel title="Dynamically load/unload multiple Modules"
height="100%"
width="100%"
paddingTop="0"
paddingLeft="0"
paddingRight="0"
paddingBottom="0"
>
<mx:TabNavigator id="navigator"
width="100%"
height="100%"
creationPolicy="all"
>
<mx:VBox id="vb1" label="Module1">
<mx:Button label="Load" click="moduleLoader(moduleLoader, l1.text)" />
<mx:Button label="Unload" click="moduleUnloader(moduleLoader)" />
<mx:Label id="l1" text="Module1.swf"/>
<mx:ModuleLoader id="moduleLoader"/>
</mx:VBox>
<mx:VBox id="vb2" label="Module2">
<mx:Button label="Load" click="moduleLoader(moduleLoader2, l2.text)"/>
<mx:Button label="Unload" click="removeModule(moduleLoader2)" />
<mx:Label id="l2" text="Module2.swf"/>
<mx:ModuleLoader id="moduleLoader2"/>
</mx:VBox>
<mx:VBox id="vb3" label="Module3">
<mx:Button label="Load" click="moduleLoader(moduleLoader3, l3.text)"/>
<mx:Button label="Unload" click="removeModule(moduleLoader3)" />
<mx:Label id="l3" text="Module3.swf"/>
<mx:ModuleLoader id="moduleLoader3"/>
</mx:VBox>
</mx:TabNavigator>
</mx:Panel>
</mx:Application>
我们当然也可以利用ModuleManager类来加载Module模块。这种方式比起纯粹的ModuleLoader方式稍微复杂一点,但是 ModuleManager提供了比ModuleLoader更加强大的能力来管理Module模块的加载过程。具体操作可以分成以下几步
通过ModuleManager实例的getModule()方法拿到Module模块的一个索引,索引类型为IModuleInfo。
调用这个索引的load()方法。
利用这个接口的factory属性拿到它相关连的Module工厂,调用此工厂的create()方法,并将返回值强制转换成当前的Module类型。
参考示例
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
4.
5. <mx:Script>
6.
7. <![CDATA[
8.
9. import mx.events.ModuleEvent;
10.
11. import mx.modules.ModuleManager;
12.
13. import mx.modules.IModuleInfo;
14.
15. public var info:IModuleInfo;
16.
17. private function init():void {
18.
19. info = ModuleManager.getModule("Module1.swf");
20.
21. info.addEventListener(ModuleEvent.READY, moduleEventHandler);
22.
23. info.load();
24.
25. }
26.
27. private function moduleEventHandler(e:ModuleEvent):void {
28.
29. vb.addChild(info.factory.create() as Module1);
30.
31. }
32.
33. ]]>
34.
35. </mx:Script>
36.
37. <mx:VBox id="vb"/>
38.
39. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import mx.modules.ModuleManager;
import mx.modules.IModuleInfo;
public var info:IModuleInfo;
private function init():void {
info = ModuleManager.getModule("Module1.swf");
info.addEventListener(ModuleEvent.READY, moduleEventHandler);
info.load();
}
private function moduleEventHandler(e:ModuleEvent):void {
vb.addChild(info.factory.create() as Module1);
}
]]>
</mx:Script>
<mx:VBox id="vb"/>
</mx:Application>
理论上说,Flex应用第一次启动时初始化下载的大小,集成了Module模块的应用相对比没有集成Module模块的相似应用要小一些。这减少了初始化页面的等待时间。甚至在Module模块的SWF文件还没有下载完毕的时候,主应用文件也可以顺利显示。Module模块第一次加载时将被缓存在客户端IE, 已缓存的Module模块被再次加载时,FlashPlayer将直接在缓存中加载Module实例,减少了加载时间,提高了用户体验。当然这有一个前提就是,客户端IE没有被清空。所以在实际操作中,你可以在SWF模块真正的被使用之前将其预加载到客户端缓存。方法是调用 ModuleManager.getModule("Module1.swf").load()语句。在调用create()方法之前并没有创建这个模块实例,而只是纯粹地将模块SWF文件加载到客户端内存。下面实例展示了在系统初始化时预先加载一个名字叫做Module1.swf的模块,当此模块需要显示时才创建这个Module模块实例。
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
4.
5. creationComplete="modulePreloader()">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. import mx.events.ModuleEvent;
12.
13. import mx.modules.ModuleManager;
14.
15. import mx.modules.IModuleInfo;
16.
17. private function modulePreloader():void {
18.
19. var info:IModuleInfo = ModuleManager.getModule("Module1.swf");
20.
21. info.addEventListener(ModuleEvent.READY, moduleEventHandler);
22.
23. info.load();
24.
25. }
26.
27. private function moduleEventHandler(e:ModuleEvent):void {
28.
29. trace('' + e.type);
30.
31. }
32.
33. ]]>
34.
35. </mx:Script>
36.
37. <mx:Panel title="Module Preloader" height="100%" width="100%" >
38.
39. <mx:TabNavigator id="tn" width="100%" height="100%" creationPolicy="all" >
40.
41. <mx:VBox id="vb1" label="Module1">
42.
43. <mx:ModuleLoader url="Module1.swf"/>
44.
45. </mx:VBox>
46.
47. </mx:TabNavigator>
48.
49. </mx:Panel>
50.
51. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="modulePreloader()">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import mx.modules.ModuleManager;
import mx.modules.IModuleInfo;
private function modulePreloader():void {
var info:IModuleInfo = ModuleManager.getModule("Module1.swf");
info.addEventListener(ModuleEvent.READY, moduleEventHandler);
info.load();
}
private function moduleEventHandler(e:ModuleEvent):void {
trace('' + e.type);
}
]]>
</mx:Script>
<mx:Panel title="Module Preloader" height="100%" width="100%" >
<mx:TabNavigator id="tn" width="100%" height="100%" creationPolicy="all" >
<mx:VBox id="vb1" label="Module1">
<mx:ModuleLoader url="Module1.swf"/>
</mx:VBox>
</mx:TabNavigator>
</mx:Panel>
</mx:Application>
怎样处理ModuleLoader事件
我们不仅可以以上述的方式加载卸载预编译Module模块,而且更进一步,也可以捕捉和处理模块在加载卸载过程中可能会触发的各种事件。这些事件由ModuleLoader触发,分别有setup,ready,loading,unload,progress,error和urlChanged 等。很好的利用这些内置的事件可以使我们更加精细地跟踪模块加载卸载过程,实现更加强大的功能。下述示例展示了一个自定义的ModuleLoader组件 CustomModuleLoader,这个组件会跟踪当模块实例被主应用加载时所有触发的事件。
Java代码
1. <?xml version="1.0" encoding="iso-8859-1"?>
2.
3. <mx:ModuleLoader xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
4.
5. creationComplete="init()">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. public function init():void {
12.
13. addEventListener("urlChanged", onUrlChanged);
14.
15. addEventListener("loading", onLoading);
16.
17. addEventListener("progress", onProgress);
18.
19. addEventListener("setup", onSetup);
20.
21. addEventListener("ready", onReady);
22.
23. addEventListener("error", onError);
24.
25. addEventListener("unload", onUnload);
26.
27. standin = panel;
28.
29. removeChild(standin);
30.
31. }
32.
33. public function onUrlChanged(event:Event):void {
34.
35. if (url == null) {
36.
37. if (contains(standin))
38.
39. removeChild(standin);
40.
41. } else {
42.
43. if (!contains(standin))
44.
45. addChild(standin);
46.
47. }
48.
49. progress.indeterminate=true;
50.
51. unload.enabled=false;
52.
53. reload.enabled=false;
54.
55. }
56.
57. public function onLoading(event:Event):void {
58.
59. progress.label="Loading module " + url;
60.
61. if (!contains(standin))
62.
63. addChild(standin);
64.
65. progress.indeterminate=true;
66.
67. unload.enabled=false;
68.
69. reload.enabled=false;
70.
71. }
72.
73. public function onProgress(event:Event):void {
74.
75. progress.label="Loaded %1 of %2 bytes...";
76.
77. progress.indeterminate=false;
78.
79. unload.enabled=true;
80.
81. reload.enabled=false;
82.
83. }
84.
85.
86.
87. public function onSetup(event:Event):void {
88.
89. progress.label="Module " + url + " initialized!";
90.
91. progress.indeterminate=false;
92.
93. unload.enabled=true;
94.
95. reload.enabled=true;
96.
97. }
98.
99. public function onReady(event:Event):void {
100.
101. progress.label="Module " + url + " successfully loaded!";
102.
103. unload.enabled=true;
104.
105. reload.enabled=true;
106.
107. if (contains(standin))
108.
109. removeChild(standin);
110.
111. }
112.
113. public function onError(event:Event):void {
114.
115. progress.label="Error loading module " + url;
116.
117. unload.enabled=false;
118.
119. reload.enabled=true;
120.
121. }
122.
123. public function onUnload(event:Event):void {
124.
125. if (url == null) {
126.
127. if (contains(standin))
128.
129. removeChild(standin);
130.
131. } else {
132.
133. if (!contains(standin))
134.
135. addChild(standin);
136.
137. }
138.
139. progress.indeterminate=true;
140.
141. progress.label="Module " + url + " was unloaded!";
142.
143. unload.enabled=false;
144.
145. reload.enabled=true;
146.
147. }
148.
149. public var standin:DisplayObject;
150.
151. ]]>
152.
153. </mx:Script>
154.
155. <mx:Panel id="panel" width="100%">
156.
157. <mx:ProgressBar width="100%" id="progress" source="{this}"/>
158.
159. <mx:HBox width="100%">
160.
161. <mx:Button id="unload"
162.
163. label="Unload Module"
164.
165. click="unloadModule()"
166.
167. />
168.
169. <mx:Button id="reload"
170.
171. label="Reload Module"
172.
173. click="unloadModule();loadModule()"
174.
175. />
176.
177. </mx:HBox>
178.
179. </mx:Panel>
180.
181. </mx:ModuleLoader>
182.
183.
184.
185.
186.
187. <?xml version="1.0"?>
188.
189. <mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
190.
191. <mx:Script>
192.
193. <![CDATA[
194.
195. [Bindable]
196.
197. public var selectedItem:Object;
198.
199. ]]>
200.
201. </mx:Script>
202.
203. <mx:ComboBox
204.
205. width="215"
206.
207. labelField="label"
208.
209. close="selectedItem=ComboBox(event.target).selectedItem"
210.
211. >
212.
213. <mx:dataProvider>
214.
215. <mx:Object label="Select Coverage"/>
216.
217. <mx:Object
218.
219. label="Life Insurance"
220.
221. module="insurancemodules/LifeInsurance.swf"
222.
223. />
224.
225. <mx:Object
226.
227. label="Auto Insurance"
228.
229. module="insurancemodules/AutoInsurance.swf"
230.
231. />
232.
233. <mx:Object
234.
235. label="Home Insurance"
236.
237. module="insurancemodules/HomeInsurance.swf"
238.
239. />
240.
241. </mx:dataProvider>
242.
243. </mx:ComboBox>
244.
245. <mx:Panel width="100%" height="100%">
246.
247. <CustomModuleLoader id="mod"
248.
249. width="100%"
250.
251. url="{selectedItem.module}"
252.
253. />
254.
255. </mx:Panel>
256.
257. <mx:HBox>
258.
259. <mx:Button label="Unload" click="mod.unloadModule()"/>
260.
261. <mx:Button label="Nullify" click="mod.url = null"/>
262.
263. </mx:HBox>
264.
265. </mx:Application>
<?xml version="1.0" encoding="iso-8859-1"?>
<mx:ModuleLoader xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
creationComplete="init()">
<mx:Script>
<![CDATA[
public function init():void {
addEventListener("urlChanged", onUrlChanged);
addEventListener("loading", onLoading);
addEventListener("progress", onProgress);
addEventListener("setup", onSetup);
addEventListener("ready", onReady);
addEventListener("error", onError);
addEventListener("unload", onUnload);
standin = panel;
removeChild(standin);
}
public function onUrlChanged(event:Event):void {
if (url == null) {
if (contains(standin))
removeChild(standin);
} else {
if (!contains(standin))
addChild(standin);
}
progress.indeterminate=true;
unload.enabled=false;
reload.enabled=false;
}
public function onLoading(event:Event):void {
progress.label="Loading module " + url;
if (!contains(standin))
addChild(standin);
progress.indeterminate=true;
unload.enabled=false;
reload.enabled=false;
}
public function onProgress(event:Event):void {
progress.label="Loaded %1 of %2 bytes...";
progress.indeterminate=false;
unload.enabled=true;
reload.enabled=false;
}
public function onSetup(event:Event):void {
progress.label="Module " + url + " initialized!";
progress.indeterminate=false;
unload.enabled=true;
reload.enabled=true;
}
public function onReady(event:Event):void {
progress.label="Module " + url + " successfully loaded!";
unload.enabled=true;
reload.enabled=true;
if (contains(standin))
removeChild(standin);
}
public function onError(event:Event):void {
progress.label="Error loading module " + url;
unload.enabled=false;
reload.enabled=true;
}
public function onUnload(event:Event):void {
if (url == null) {
if (contains(standin))
removeChild(standin);
} else {
if (!contains(standin))
addChild(standin);
}
progress.indeterminate=true;
progress.label="Module " + url + " was unloaded!";
unload.enabled=false;
reload.enabled=true;
}
public var standin:DisplayObject;
]]>
</mx:Script>
<mx:Panel id="panel" width="100%">
<mx:ProgressBar width="100%" id="progress" source="{this}"/>
<mx:HBox width="100%">
<mx:Button id="unload"
label="Unload Module"
click="unloadModule()"
/>
<mx:Button id="reload"
label="Reload Module"
click="unloadModule();loadModule()"
/>
</mx:HBox>
</mx:Panel>
</mx:ModuleLoader>
<?xml version="1.0"?>
<mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var selectedItem:Object;
]]>
</mx:Script>
<mx:ComboBox
width="215"
labelField="label"
close="selectedItem=ComboBox(event.target).selectedItem"
>
<mx:dataProvider>
<mx:Object label="Select Coverage"/>
<mx:Object
label="Life Insurance"
module="insurancemodules/LifeInsurance.swf"
/>
<mx:Object
label="Auto Insurance"
module="insurancemodules/AutoInsurance.swf"
/>
<mx:Object
label="Home Insurance"
module="insurancemodules/HomeInsurance.swf"
/>
</mx:dataProvider>
</mx:ComboBox>
<mx:Panel width="100%" height="100%">
<CustomModuleLoader id="mod"
width="100%"
url="{selectedItem.module}"
/>
</mx:Panel>
<mx:HBox>
<mx:Button label="Unload" click="mod.unloadModule()"/>
<mx:Button label="Nullify" click="mod.url = null"/>
</mx:HBox>
</mx:Application>
应用error事件
使用ModuleLoader的error事件可以允许开发者在当不知什么原因Module模块没有被加载或卸载成功的时候,做一些必要的动作。参考以下示例
Java代码
1. <?xml version="1.0"?>
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Script>
6.
7. <![CDATA[
8.
9. import mx.events.ModuleEvent;
10.
11. import mx.modules.*;
12.
13. import mx.controls.Alert;
14.
15. private function errorHandler(e:ModuleEvent):void {
16.
17. Alert.show("There was an error loading the module." +
18.
19. " Please contact the Help Desk.");
20.
21. trace(e.errorText);
22.
23. }
24.
25. public function createModule():void {
26.
27. if (chartModuleLoader.url == ti1.text) {
28.
29. // If they are the same, call loadModule.
30.
31. chartModuleLoader.loadModule();
32.
33. } else {
34.
35. // If they are not the same, then change the url,
36.
37. // which triggers a call to the loadModule() method.
38.
39. chartModuleLoader.url = ti1.text;
40.
41. }
42.
43. }
44.
45. public function removeModule():void {
46.
47. chartModuleLoader.unloadModule();
48.
49. }
50.
51. ]]>
52.
53. </mx:Script>
54.
55. <mx:Panel title="Module Example"
56.
57. height="90%"
58.
59. width="90%"
60.
61. paddingTop="10"
62.
63. paddingLeft="10"
64.
65. paddingRight="10"
66.
67. paddingBottom="10"
68.
69. >
70.
71. <mx:HBox>
72.
73. <mx:Label text="URL:"/>
74.
75. <mx:TextInput width="200" id="ti1" text="ColumnChartModule.swf"/
76.
77. >
78.
79. <mx:Button label="Load" click="createModule()"/>
80.
81. <mx:Button label="Unload" click="removeModule()"/>
82.
83. </mx:HBox>
84.
85. <mx:ModuleLoader id="chartModuleLoader" error="errorHandler(event)"/
86.
87. >
88.
89. </mx:Panel>
90.
91. </mx:Application>
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import mx.modules.*;
import mx.controls.Alert;
private function errorHandler(e:ModuleEvent):void {
Alert.show("There was an error loading the module." +
" Please contact the Help Desk.");
trace(e.errorText);
}
public function createModule():void {
if (chartModuleLoader.url == ti1.text) {
// If they are the same, call loadModule.
chartModuleLoader.loadModule();
} else {
// If they are not the same, then change the url,
// which triggers a call to the loadModule() method.
chartModuleLoader.url = ti1.text;
}
}
public function removeModule():void {
chartModuleLoader.unloadModule();
}
]]>
</mx:Script>
<mx:Panel title="Module Example"
height="90%"
width="90%"
paddingTop="10"
paddingLeft="10"
paddingRight="10"
paddingBottom="10"
>
<mx:HBox>
<mx:Label text="URL:"/>
<mx:TextInput width="200" id="ti1" text="ColumnChartModule.swf"/
>
<mx:Button label="Load" click="createModule()"/>
<mx:Button label="Unload" click="removeModule()"/>
</mx:HBox>
<mx:ModuleLoader id="chartModuleLoader" error="errorHandler(event)"/
>
</mx:Panel>
</mx:Application>
应用progress事件
使用ModuleLoader的progress事件可以让你跟踪模块实例加载的进度信息。参考示例,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/SimpleProgressEventHandler.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. import mx.events.ModuleEvent;
12.
13. import flash.events.ProgressEvent;
14.
15. import mx.modules.*;
16.
17. [Bindable]
18.
19. public var progBar:String = "";
20.
21. [Bindable]
22.
23. public var progMessage:String = "";
24.
25. private function progressEventHandler(e:ProgressEvent):void {
26.
27. progBar += ".";
28.
29. progMessage =
30.
31. "Module " +
32.
33. Math.round((e.bytesLoaded/e.bytesTotal) * 100) +
34.
35. "% loaded";
36.
37. }
38.
39. public function createModule():void {
40.
41. chartModuleLoader.loadModule();
42.
43. }
44.
45. public function removeModule():void {
46.
47. chartModuleLoader.unloadModule();
48.
49. progBar = "";
50.
51. progMessage = "";
52.
53. }
54.
55. ]]>
56.
57. </mx:Script>
58.
59. <mx:Panel title="Module Example"
60.
61. height="90%"
62.
63. width="90%"
64.
65. paddingTop="10"
66.
67. paddingLeft="10"
68.
69. paddingRight="10"
70.
71. paddingBottom="10"
72.
73. >
74.
75. <mx:HBox>
76.
77. <mx:Label id="l2" text="{progMessage}"/>
78.
79. <mx:Label id="l1" text="{progBar}"/>
80.
81. </mx:HBox>
82.
83. <mx:Button label="Load" click="createModule()"/>
84.
85. <mx:Button label="Unload" click="removeModule()"/>
86.
87. <mx:ModuleLoader
88.
89. id="chartModuleLoader"
90.
91. url="ColumnChartModule.swf"
92.
93. progress="progressEventHandler(event)"
94.
95. />
96.
97. </mx:Panel>
98.
99. </mx:Application>
<?xml version="1.0"?>
<!-- modules/SimpleProgressEventHandler.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.ModuleEvent;
import flash.events.ProgressEvent;
import mx.modules.*;
[Bindable]
public var progBar:String = "";
[Bindable]
public var progMessage:String = "";
private function progressEventHandler(e:ProgressEvent):void {
progBar += ".";
progMessage =
"Module " +
Math.round((e.bytesLoaded/e.bytesTotal) * 100) +
"% loaded";
}
public function createModule():void {
chartModuleLoader.loadModule();
}
public function removeModule():void {
chartModuleLoader.unloadModule();
progBar = "";
progMessage = "";
}
]]>
</mx:Script>
<mx:Panel title="Module Example"
height="90%"
width="90%"
paddingTop="10"
paddingLeft="10"
paddingRight="10"
paddingBottom="10"
>
<mx:HBox>
<mx:Label id="l2" text="{progMessage}"/>
<mx:Label id="l1" text="{progBar}"/>
</mx:HBox>
<mx:Button label="Load" click="createModule()"/>
<mx:Button label="Unload" click="removeModule()"/>
<mx:ModuleLoader
id="chartModuleLoader"
url="ColumnChartModule.swf"
progress="progressEventHandler(event)"
/>
</mx:Panel>
</mx:Application>
怎样共享和传输Module模块间数据
Module模块是一个容器,每个独立的模块对象都相当于一个自定义组件。有以下几种方式可以实现模块-模块、模块-主应用、主应用-模块、模块-一般自定义组件间的数据传输和通信。
利用ModuleLoader的child、ModuleManager的factory、以及Application的parentApplication属性存取Module模块和主应用文件对象索引。
因为Module模块在ModuleLoader中通常用url属性来指定,所以我们可以通过在url上面拼GET参数,然后在Module模块中解析这些拼上去的参数的方式来传输数据。
通过ActionScript接口方式。你可以定义一个ActionScript接口,这个接口定义了一系列方法和属性。Module模块和主应用Application都可以访问这些属性。从而实现数据共享。
在主应用Application中访问Module模块对象
我们可以在主应用Application中访问子Module模块对象中定义的方法和属性。下面示例展示了通过ModuleLoader的child属性拿到子Module模块对象索引,然后调用其定义的公共方法getTitle().
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/ParentApplication.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script><![CDATA[
8.
9. [Bindable]
10.
11. private var s:String;
12.
13. private function getTitle():void {
14.
15. s = (m1.child as ChildModule1).getModTitle();
16.
17. }
18.
19. ]]></mx:Script>
20.
21. <mx:Label id="l1" text="{s}"/>
22.
23. <mx:ModuleLoader url="ChildModule1.swf" id="m1" ready="getTitle()"/>
24.
25. </mx:Application>
<?xml version="1.0"?>
<!-- modules/ParentApplication.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
[Bindable]
private var s:String;
private function getTitle():void {
s = (m1.child as ChildModule1).getModTitle();
}
]]></mx:Script>
<mx:Label id="l1" text="{s}"/>
<mx:ModuleLoader url="ChildModule1.swf" id="m1" ready="getTitle()"/>
</mx:Application>
Module模块文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/ChildModule1.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%">
8.
9. <mx:Script><![CDATA[
10.
11. // Defines the method that the application calls.
12.
13. public function getModTitle():String {
14.
15. return "Child Module 1";
16.
17. }
18.
19. ]]></mx:Script>
20.
21. </mx:Module>
<?xml version="1.0"?>
<!-- modules/ChildModule1.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%">
<mx:Script><![CDATA[
// Defines the method that the application calls.
public function getModTitle():String {
return "Child Module 1";
}
]]></mx:Script>
</mx:Module>
通过这种方式得到子Module模块的索引导致了主应用Application和Module模块之间的紧耦合,不利于模块逻辑的重用。同样地,利用ModuleManager的factory属性也可以拿到当前ModuleLoader的子模块对象的索引。参考以下示例,模块文件为,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/mxmlmodules/SimpleModule.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. public function computeAnswer(a:Number, b:Number):Number {
12.
13. return a + b;
14.
15. }
16.
17. ]]>
18.
19. </mx:Script>
20.
21. </mx:Module>
<?xml version="1.0"?>
<!-- modules/mxmlmodules/SimpleModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
public function computeAnswer(a:Number, b:Number):Number {
return a + b;
}
]]>
</mx:Script>
</mx:Module>
主应用Application为,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/mxmlmodules/SimpleMXMLApp.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
6.
7. creationComplete="initApp()">
8.
9. <mx:Script>
10.
11. <![CDATA[
12.
13. import mx.modules.IModuleInfo;
14.
15. import mx.modules.ModuleManager;
16.
17. public var assetModule:IModuleInfo;
18.
19. public var sm:Object;
20.
21. [Bindable]
22.
23. public var answer:Number = 0;
24.
25. public function initApp():void {
26.
27. // Get the IModuleInfo interface for the specified URL.
28.
29. assetModule = ModuleManager.getModule("SimpleModule.swf");
30.
31. assetModule.addEventListener("ready", getModuleInstance);
32.
33. assetModule.load();
34.
35. }
36.
37. public function getModuleInstance(e:Event):void {
38.
39. // Get an instance of the module.
40.
41. sm = assetModule.factory.create() as SimpleModule;
42.
43. }
44.
45. public function addNumbers():void {
46.
47. var a:Number = Number(ti1.text);
48.
49. var b:Number = Number(ti2.text);
50.
51. // Call a method on the module.
52.
53. answer = sm.computeAnswer(a, b).toString();
54.
55. }
56.
57. ]]>
58.
59. </mx:Script>
60.
61. <mx:Form>
62.
63. <mx:FormHeading label="Enter values to sum."/>
64.
65. <mx:FormItem label="First Number">
66.
67. <mx:TextInput id="ti1" width="50"/>
68.
69. </mx:FormItem>
70.
71. <mx:FormItem label="Second Number">
72.
73. <mx:TextInput id="ti2" width="50"/>
74.
75. </mx:FormItem>
76.
77. <mx:FormItem label="Result">
78.
79. <mx:Label id="ti3" width="100" text="{answer}"/>
80.
81. </mx:FormItem>
82.
83. <mx:Button id="b1" label="Compute" click="addNumbers()"/>
84.
85. </mx:Form>
86.
87. </mx:Application>
<?xml version="1.0"?>
<!-- modules/mxmlmodules/SimpleMXMLApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.modules.IModuleInfo;
import mx.modules.ModuleManager;
public var assetModule:IModuleInfo;
public var sm:Object;
[Bindable]
public var answer:Number = 0;
public function initApp():void {
// Get the IModuleInfo interface for the specified URL.
assetModule = ModuleManager.getModule("SimpleModule.swf");
assetModule.addEventListener("ready", getModuleInstance);
assetModule.load();
}
public function getModuleInstance(e:Event):void {
// Get an instance of the module.
sm = assetModule.factory.create() as SimpleModule;
}
public function addNumbers():void {
var a:Number = Number(ti1.text);
var b:Number = Number(ti2.text);
// Call a method on the module.
answer = sm.computeAnswer(a, b).toString();
}
]]>
</mx:Script>
<mx:Form>
<mx:FormHeading label="Enter values to sum."/>
<mx:FormItem label="First Number">
<mx:TextInput id="ti1" width="50"/>
</mx:FormItem>
<mx:FormItem label="Second Number">
<mx:TextInput id="ti2" width="50"/>
</mx:FormItem>
<mx:FormItem label="Result">
<mx:Label id="ti3" width="100" text="{answer}"/>
</mx:FormItem>
<mx:Button id="b1" label="Compute" click="addNumbers()"/>
</mx:Form>
</mx:Application>
在Module模块对象中存取主应用Application
Module模块可以通过其parentApplication属性拿到主应用Application对象的索引,通过这个索引可以访问主应用对象的公共方法和属性。参考以下示例,Module模块文件为,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/ChartChildModule.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%" creationComplete="getDataFromParent()">
8.
9. <mx:Script><![CDATA[
10.
11. import mx.collections.ArrayCollection;
12.
13. [Bindable]
14.
15. private var expenses:ArrayCollection;
16.
17. // Access properties of the parent application.
18.
19. private function getDataFromParent():void {
20.
21. expenses = parentApplication.expenses;
22.
23. }
24.
25. ]]></mx:Script>
26.
27. <mx:ColumnChart id="myChart" dataProvider="{expenses}">
28.
29. <mx:horizontalAxis>
30.
31. <mx:CategoryAxis
32.
33. dataProvider="{expenses}"
34.
35. categoryField="Month"
36.
37. />
38.
39. </mx:horizontalAxis>
40.
41. <mx:series>
42.
43. <mx:ColumnSeries
44.
45. xField="Month"
46.
47. yField="Profit"
48.
49. displayName="Profit"
50.
51. />
52.
53. <mx:ColumnSeries
54.
55. xField="Month"
56.
57. yField="Expenses"
58.
59. displayName="Expenses"
60.
61. />
62.
63. </mx:series>
64.
65. </mx:ColumnChart>
66.
67. <mx:Legend dataProvider="{myChart}"/>
68.
69. <mx:Button id="b1" click="expenses = parentApplication.getNewData();"
70.
71. label="Get New Data"/>
72.
73. </mx:Module>
<?xml version="1.0"?>
<!-- modules/ChartChildModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%" creationComplete="getDataFromParent()">
<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var expenses:ArrayCollection;
// Access properties of the parent application.
private function getDataFromParent():void {
expenses = parentApplication.expenses;
}
]]></mx:Script>
<mx:ColumnChart id="myChart" dataProvider="{expenses}">
<mx:horizontalAxis>
<mx:CategoryAxis
dataProvider="{expenses}"
categoryField="Month"
/>
</mx:horizontalAxis>
<mx:series>
<mx:ColumnSeries
xField="Month"
yField="Profit"
displayName="Profit"
/>
<mx:ColumnSeries
xField="Month"
yField="Expenses"
displayName="Expenses"
/>
</mx:series>
</mx:ColumnChart>
<mx:Legend dataProvider="{myChart}"/>
<mx:Button id="b1" click="expenses = parentApplication.getNewData();"
label="Get New Data"/>
</mx:Module>
主应用文件为,
<?xml version="1.0"?>
Java代码
1. <!-- modules/ChartChildModuleLoader.mxml -->
2.
3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
4.
5. <mx:Script><![CDATA[
6.
7. import mx.collections.ArrayCollection;
8.
9. [Bindable]
10.
11. public var expenses:ArrayCollection = new ArrayCollection([
12.
13. {Month:"Jan", Profit:2000, Expenses:1500},
14.
15. {Month:"Feb", Profit:1000, Expenses:200},
16.
17. {Month:"Mar", Profit:1500, Expenses:500}
18.
19. ]);
20.
21. public function getNewData():ArrayCollection {
22.
23. return new ArrayCollection([
24.
25. {Month:"Apr", Profit:1000, Expenses:1100},
26.
27. {Month:"May", Profit:1300, Expenses:500},
28.
29. {Month:"Jun", Profit:1200, Expenses:600}
30.
31. ]);
32.
33. }
34.
35. ]]></mx:Script>
36.
37. <mx:ModuleLoader url="ChartChildModule.swf" id="m1"/>
38.
39. </mx:Application>
<!-- modules/ChartChildModuleLoader.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var expenses:ArrayCollection = new ArrayCollection([
{Month:"Jan", Profit:2000, Expenses:1500},
{Month:"Feb", Profit:1000, Expenses:200},
{Month:"Mar", Profit:1500, Expenses:500}
]);
public function getNewData():ArrayCollection {
return new ArrayCollection([
{Month:"Apr", Profit:1000, Expenses:1100},
{Month:"May", Profit:1300, Expenses:500},
{Month:"Jun", Profit:1200, Expenses:600}
]);
}
]]></mx:Script>
<mx:ModuleLoader url="ChartChildModule.swf" id="m1"/>
</mx:Application>
不过这种方式导致的一个缺点是,自定义的Module模块文件的可移植特性将大打折扣。
在一个Module模块对象中存取另一个Module模块对象
同样地,我们也可以利用ModuleLoader的child属性,在一个Module模块对象中拿到另一个Module模块对象的索引,通过这个索引访问当前Module模块对象的公共方法和属性。参考以下示例,主应用文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/TitleModuleLoader.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script><![CDATA[
8.
9. ]]></mx:Script>
10.
11. <mx:ModuleLoader url="InterModule1.swf" id="m1"/>
12.
13. <mx:ModuleLoader url="InterModule2.swf" id="m2"/>
14.
15. </mx:Application>
<?xml version="1.0"?>
<!-- modules/TitleModuleLoader.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
]]></mx:Script>
<mx:ModuleLoader url="InterModule1.swf" id="m1"/>
<mx:ModuleLoader url="InterModule2.swf" id="m2"/>
</mx:Application>
Module1文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/InterModule1.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%">
8.
9. <mx:Script><![CDATA[
10.
11. // Defines the method that the other module calls.
12.
13. public function getNewTitle():String {
14.
15. return "New Module Title";
16.
17. }
18.
19. ]]></mx:Script>
20.
21. </mx:Module>
<?xml version="1.0"?>
<!-- modules/InterModule1.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%">
<mx:Script><![CDATA[
// Defines the method that the other module calls.
public function getNewTitle():String {
return "New Module Title";
}
]]></mx:Script>
</mx:Module>
Module2文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/InterModule2.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
6.
7. height="100%">
8.
9. <mx:Script><![CDATA[
10.
11. [Bindable]
12.
13. private var title:String;
14.
15. // Call method of another module.
16.
17. private function changeTitle():void {
18.
19. title = parentApplication.m1.child.getNewTitle();
20.
21. }
22.
23. ]]></mx:Script>
24.
25. <mx:HBox>
26.
27. <mx:Label id="l1" text="Title: "/>
28.
29. <mx:Label id="myTitle" text="{title}"/>
30.
31. </mx:HBox>
32.
33. <mx:Button id="b1" label="Change Title" click="changeTitle()"/>
34.
35. </mx:Module>
<?xml version="1.0"?>
<!-- modules/InterModule2.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%">
<mx:Script><![CDATA[
[Bindable]
private var title:String;
// Call method of another module.
private function changeTitle():void {
title = parentApplication.m1.child.getNewTitle();
}
]]></mx:Script>
<mx:HBox>
<mx:Label id="l1" text="Title: "/>
<mx:Label id="myTitle" text="{title}"/>
</mx:HBox>
<mx:Button id="b1" label="Change Title" click="changeTitle()"/>
</mx:Module>
通过拼ModuleLoader的url参数方式实现数据传输
在url上面拼GET参数基本上是这种格式,url=module1.swf?param1=value1¶m2=value2
比如在主应用文件Application中拼一系列GET参数到ModuleLoader的url属性上,在Module模块文件中解析并处理这些参数,参考以下示例,主应用文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/QueryStringApp.mxml -->
4.
5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="500"
6.
7. width="400">
8.
9. <mx:Script><![CDATA[
10.
11. public function initModule():void {
12.
13. // Build query string so that it looks something like this:
14.
15. // "QueryStringModule.swf?firstName=Nick&lastName=Danger"
16.
17. var s:String = "QueryStringModule.swf?" + "firstName=" +
18.
19. ti1.text + "&lastName=" + ti2.text;
20.
21. // Changing the url property of the ModuleLoader causes
22.
23. // the ModuleLoader to load a new module.
24.
25. m1.url = s;
26.
27. }
28.
29. ]]></mx:Script>
30.
31. <mx:Form>
32.
33. <mx:FormItem id="fi1" label="First Name:">
34.
35. <mx:TextInput id="ti1"/>
36.
37. </mx:FormItem>
38.
39. <mx:FormItem id="fi2" label="Last Name:">
40.
41. <mx:TextInput id="ti2"/>
42.
43. </mx:FormItem>
44.
45. </mx:Form>
46.
47. <mx:ModuleLoader id="m1"/>
48.
49. <mx:Button id="b1" label="Submit" click="initModule()"/>
50.
51. </mx:Application>
<?xml version="1.0"?>
<!-- modules/QueryStringApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="500"
width="400">
<mx:Script><![CDATA[
public function initModule():void {
// Build query string so that it looks something like this:
// "QueryStringModule.swf?firstName=Nick&lastName=Danger"
var s:String = "QueryStringModule.swf?" + "firstName=" +
ti1.text + "&lastName=" + ti2.text;
// Changing the url property of the ModuleLoader causes
// the ModuleLoader to load a new module.
m1.url = s;
}
]]></mx:Script>
<mx:Form>
<mx:FormItem id="fi1" label="First Name:">
<mx:TextInput id="ti1"/>
</mx:FormItem>
<mx:FormItem id="fi2" label="Last Name:">
<mx:TextInput id="ti2"/>
</mx:FormItem>
</mx:Form>
<mx:ModuleLoader id="m1"/>
<mx:Button id="b1" label="Submit" click="initModule()"/>
</mx:Application>
模块文件,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/QueryStringModule.mxml -->
4.
5. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"
6.
7. creationComplete="parseString()">
8.
9. <mx:Script>
10.
11. <![CDATA[
12.
13. import mx.utils.*;
14.
15. [Bindable]
16.
17. private var salutation:String;
18.
19. public var o:Object = {};
20.
21. public function parseString():void {
22.
23. try {
24.
25. // Remove everything before the question mark, including
26.
27. // the question mark.
28.
29. var myPattern:RegExp = /.*\?/;
30.
31. var s:String = this.loaderInfo.url.toString();
32.
33. s = s.replace(myPattern, "");
34.
35. // Create an Array of name=value Strings.
36.
37. var params:Array = s.split("&");
38.
39. // Print the params that are in the Array.
40.
41. var keyStr:String;
42.
43. var valueStr:String;
44.
45. var paramObj:Object = params;
46.
47. for (keyStr in paramObj) {
48.
49. valueStr = String(paramObj[keyStr]);
50.
51. ta1.text += keyStr + ":" + valueStr + "\n";
52.
53. }
54.
55. // Set the values of the salutation.
56.
57. for (var i:int = 0; i < params.length; i++) {
58.
59. var tempA:Array = params[i].split("=");
60.
61. if (tempA[0] == "firstName") {
62.
63. o.firstName = tempA[1];
64.
65. }
66.
67. if (tempA[0] == "lastName") {
68.
69. o.lastName = tempA[1];
70.
71. }
72.
73. }
74.
75. if (StringUtil.trim(o.firstName) != "" &&
76.
77. StringUtil.trim(o.lastName) != "") {
78.
79. salutation = "Welcome " +
80.
81. o.firstName + " " + o.lastName + "!";
82.
83. } else {
84.
85. salutation = "Full name not entered."
86.
87. }
88.
89. } catch (e:Error) {
90.
91. trace(e);
92.
93. }
94.
95. // Show some of the information available through loaderInfo:
96.
97. trace("AS version: " + this.loaderInfo.actionScriptVersion);
98.
99. trace("App height: " + this.loaderInfo.height);
100.
101. trace("App width: " + this.loaderInfo.width);
102.
103. trace("App bytes: " + this.loaderInfo.bytesTotal);
104.
105. }
106.
107. ]]>
108.
109. </mx:Script>
110.
111. <mx:Label text="{salutation}"/>
112.
113. <mx:TextArea height="100" width="300" id="ta1"/>
114.
115. </mx:Module>
<?xml version="1.0"?>
<!-- modules/QueryStringModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="parseString()">
<mx:Script>
<![CDATA[
import mx.utils.*;
[Bindable]
private var salutation:String;
public var o:Object = {};
public function parseString():void {
try {
// Remove everything before the question mark, including
// the question mark.
var myPattern:RegExp = /.*\?/;
var s:String = this.loaderInfo.url.toString();
s = s.replace(myPattern, "");
// Create an Array of name=value Strings.
var params:Array = s.split("&");
// Print the params that are in the Array.
var keyStr:String;
var valueStr:String;
var paramObj:Object = params;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]);
ta1.text += keyStr + ":" + valueStr + "\n";
}
// Set the values of the salutation.
for (var i:int = 0; i < params.length; i++) {
var tempA:Array = params[i].split("=");
if (tempA[0] == "firstName") {
o.firstName = tempA[1];
}
if (tempA[0] == "lastName") {
o.lastName = tempA[1];
}
}
if (StringUtil.trim(o.firstName) != "" &&
StringUtil.trim(o.lastName) != "") {
salutation = "Welcome " +
o.firstName + " " + o.lastName + "!";
} else {
salutation = "Full name not entered."
}
} catch (e:Error) {
trace(e);
}
// Show some of the information available through loaderInfo:
trace("AS version: " + this.loaderInfo.actionScriptVersion);
trace("App height: " + this.loaderInfo.height);
trace("App width: " + this.loaderInfo.width);
trace("App bytes: " + this.loaderInfo.bytesTotal);
}
]]>
</mx:Script>
<mx:Label text="{salutation}"/>
<mx:TextArea height="100" width="300" id="ta1"/>
</mx:Module>
利用ActionScript接口实现Module模块间的数据通信
在面向对象的编程中,我们讲要面向接口编程。面向接口的编程方式从一定程度上解决了相互关联的模块间的紧密耦合问题。以上提到的所有数据传输和共享方式都在不同程度上导致了模块间的紧耦合。不过,Flex提供了一种利用标准的ActionScript接口实现Module模块间数据通信的方式。具体地说,对于Module模块对象和主应用Application对象间的通信,我们可以定义一个ActionScript接口,Module模块对象实现了这个接口中定义的方法和属性,那么主应用Application就可以访问这个接口中定义的属性和方法。接口中定义了Module模块对象和主应用 Application需要共享的数据和方法,是两者间共同的一个契约,同时也实现了接口和实现的分离,达到了松耦合的目的。参考以下示例,主应用 Application,
Java代码
1. <?xml version="1.0"?>
2.
3. <!-- modules/interfaceexample/Main.mxml -->
4.
5. <mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
6.
7. <mx:Script>
8.
9. <![CDATA[
10.
11. import mx.events.ModuleEvent;
12.
13. import mx.modules.ModuleManager;
14.
15. [Bindable]
16.
17. public var selectedItem:Object;
18.
19. [Bindable]
20.
21. public var currentModuleName:String;
22.
23. private function applyModuleSettings(e:Event):void {
24.
25. // Cast the ModuleLoader's child to the interface.
26.
27. // This child is an instance of the module.
28.
29. // You can now call methods on that instance.
30.
31. var ichild:* = mod.child as IModuleInterface;
32.
33. if (mod.child != null) {
34.
35. // Call setters in the module to adjust its
36.
37. // appearance when it loads.
38.
39. ichild.setAdjusterID(myId.text);
40.
41. ichild.setBackgroundColor(myColor.selectedColor);
42.
43. } else {
44.
45. trace("Uh oh. The mod.child property is null");
46.
47. }
48.
49. // Set the value of a local variable by calling a method
50.
51. // on the interface.
52.
53. currentModuleName = ichild.getModuleName();
54.
55. }
56.
57. private function reloadModule():void {
58.
59. mod.unloadModule();
60.
61. mod.loadModule();
62.
63. }
64.
65. ]]>
66.
67. </mx:Script>
68.
69. <mx:Form>
70.
71. <mx:FormItem label="Current Module:">
72.
73. <mx:Label id="l1" text="{currentModuleName}"/>
74.
75. </mx:FormItem>
76.
77. <mx:FormItem label="Adjuster ID:">
78.
79. <mx:TextInput id="myId" text="Enter your ID"/>
80.
81. </mx:FormItem>
82.
83. <mx:FormItem label="Background Color:">
84.
85. <mx:ColorPicker id="myColor"
86.
87. selectedColor="0xFFFFFF"
88.
89. change="reloadModule()"
90.
91. />
92.
93. </mx:FormItem>
94.
95. </mx:Form>
96.
97. <mx:Label text="Long Shot Insurance" fontSize="24"/>
98.
99. <mx:ComboBox