Flex Module通信(2)——使用事件

上一篇讨 论通过实现接口来实现Application和Modules的通信,本篇讨论通过使用事件实现Application和Modules的通信。在一个 swf中,组件间的交互通信可以直接使用addEventListener和dispatchEvent来完成事件的传递。当然这样的标准做法也适用于 Module。

      通过addEventListener和dispatchEvent在Module发布事件:

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onComplete()">
 3:   <mx:Script>
 4:   <![CDATA[
 5:              import mx.events.DynamicEvent;
 6:   
 7:              [Bindable]private var command:String="";
 8:              private function onComplete():void
 9:   {
                     //从Application订阅
10:                  var sharedEventDispatcher:IEventDispatcher=systemManager.loaderInfo.sharedEvents;
11:   
12:                  sharedEventDispatcher.addEventListener("init",onInit,false,0,true);
13:                  //通知Application:Module创建完成
14:                  sharedEventDispatcher.dispatchEvent(new Event(Event.COMPLETE));
15:              }
16:    
17:              private function onInit(event:DynamicEvent):void
18:   {
19:                  command=event.data as String;
20:              }
21:   
22:          ]]>
23:      </mx:Script>
24:   
25:   <mx:Panel id="panel" title="Module With Events. {command}" width="400"  height="200"/>
26:  </mx:Module>
27:   

 

        运行结果,LoadModule后显示从Application传入的内容:

image

你可能感兴趣的:(Module)