在这里主要说明的是 在组件和模块之间传递事件是需要模块同时继承<viewer:BaseWigdet></viewer:BaseWigdet>才可以监听到事件。
wigdet本身的BaseWigdet就是一个模块,它是继承Module的。所以说你们如果是要在继承BaseWigdet的模块派发事件,然后在没有继承BaseWigdet的自定义组件里是监听不到的。如果你这样做的话,想实现事件监听那就把自定义组件去继承BaseWigdet就可以了。
自定义事件类 UploadEvent.as
2 {
3 /**
4 * @Content 上传 事件类
5 * @Author sange
6 * @E-mail: sange.chen@gmail.com
7 * @Version 1.0.0
8 * @Date:Apr 12, 2012 6:52:25 PM
9 */
10 import flash.events.Event;
11 import flash.events.EventDispatcher;
12
13 public class UploadEvent extends Event
14 {
15 private var _data:Object;
16
17 public static const UPLOAD_COMPLETE:String = "uploadComplete";
18 public static const UPLOADVIEW:String = "uploadView";
19 public static const UPLOAD_SECURITY_ERROR:String="uploadSecurityError";
20 public static const UPLOAD_ERROR:String = "uploadIoError";
21
22 public static var dispatcher:EventDispatcher = new EventDispatcher();
23
24 public function UploadEvent(obj:Object ,type:String, bubbles:Boolean=false, cancelable:Boolean=false)
25 {
26 _data = obj;
27 super(type, bubbles, cancelable);
28 }
29
30 public function get data():Object
31 {
32 return _data;
33 }
34
35 override public function clone():Event
36 {
37 return new UploadEvent(_data,type,bubbles,cancelable);
38 }
39 }
40 }
看到定义了一个静态变量 dispatcher,是EventDispatcher类型,用dispatcher
可以任何地方监听到。
Main.mxml
2 PopUpManager.centerPopUp(panl);
3 UploadEvent.dispatcher.dispatchEvent(new UploadEvent(__uploadFileUrl,UploadEvent.UPLOADVIEW));
FileUploadWidget.mxml
2 {
3 UploadEvent.dispatcher.addEventListener(UploadEvent.UPLOADVIEW,uploadViewHandler);
4
5 _arrUploadFiles = new ArrayCollection();
6 enableUI();
7 uploadCheck();
8 }
9 private function uploadViewHandler(e:UploadEvent):void
10 {
11 _strUploadUrl = new String(e.data);
12 }