There are many ways to communicate with Flex Modules: communication by continuing interfaces, passing parameters, and defining events. We will discuss communication through the continuation interface. Modules are independent and allow external applications to communicate with them through an inherited interface.
First, define the interface ICommunicaton.:
1: package
2: {
3: public interface ICommunication
4: {
5: function getMessage():String;
6:
7: function setMessage(value:String):void;
8: }
9: }
Create a Module to inherit the ICommunicaton interface:
1: <?xml version="1.0" encoding="utf-8"?>
2: <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" implements="ICommunication" >
3: <mx:Script>
4: <![CDATA[
5: [Bindable]private var _value:String="";
6:
7: public function setMessage(value:String):void
8: {
9: _value=value;
10: }
11:
12: public function getMessage():String
13: {
14: return _value;
15: }
16: ]]>
17: </mx:Script>
18:
19: <mx:Panel id="panel" title="Message :{_value}" width="400" height="200"/>
20: </mx:Module>
21:
In Application, the Module method is called through ICommunication:
1: var communication:ICommunication=moduleLoader.child as ICommunication;
2: communication.setMessage("loaded by application");
Application code:
1: <?xml version="1.0" encoding="utf-8"?>
2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3: <mx:Script>
4: <![CDATA[
5: import mx.containers.Panel;
6: import mx.modules.Module;
7:
8: private const MODULE_URL:String="Module1.swf";
9:
10: private function onModifyMessage():void
11: {
12: var communication:ICommunication=moduleLoader.child as ICommunication;
13: communication.setMessage("loaded by application");
14:
15: var module:Module = moduleLoader.child as Module;
16: var panel:Panel=module.getChildByName("panel") as Panel;
17: trace(panel.title);
18:
19: }
20: ]]>
21: </mx:Script>
22:
23: <mx:HBox>
24: <mx:Button id="btnLoad" label="Load Module" click="moduleLoader.loadModule(MODULE_URL)" />
25: <mx:Button id="btnModify" label="Modify Module" click="onModifyMessage()"/>
26: <mx:Button label="Unload Module" click="moduleLoader.unloadModule()" />
27: </mx:HBox>
28: <mx:ModuleLoader id="moduleLoader" y="30"/>
29: </mx:Application>
30:
Run the result. After loading the result, click Modify Module:
Note: although it is convenient to use the inherited interface to implement the communication between Application and Modules, the Modules memory will be locked when the Modules method is called.