创建Flex事件总线AppEvent

来源:互联网 发布:ubuntu 中英文切换 编辑:程序博客网 时间:2024/05/17 01:27

为了使程序各模块间达到高内聚低耦合,各模块间的事件传递一般采用事件总线方式,即将要传递的事件派发到事件总线进行广播,然后在需要接收事件的模块中进行监听,这样就能使模块结构更加清晰,从代码结构上看,也更加符合软件设计标准。

创建事件总线控制类AppEvent.as及EventBus.as

package com.neil{import flash.events.Event;public class AppEvent extends Event{/*事件总线测试*/public static const TEST_EVENT:String="testevent";private var _data:Object;private var _callback:Function;public function get callback():Function{return _callback;}public function set callback(value:Function):void{_callback = value;}public function get data():Object{return _data;}public function set data(value:Object):void{_data = value;}public function AppEvent(type:String,data:Object=null,callback:Function=null){super(type);_data=data;_callback=callback;}/** * Override clone */public override function clone():Event{return new AppEvent(this.type,this.data,this.callback);}/** * Dispatch this event. */public function dispatch():Boolean{return EventBus.instance.dispatchEvent(this);}public static function dispatch(type:String, data:Object = null, callback:Function = null):Boolean{return EventBus.instance.dispatchEvent(new AppEvent(type, data, callback));}public static function addListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{EventBus.instance.addEventListener(type, listener, useCapture, priority, useWeakReference);}public static function removeListener(type:String, listener:Function, useCapture:Boolean = false):void{EventBus.instance.removeEventListener(type, listener, useCapture);}}}

EventBus.as类:

package com.neil{import flash.events.Event;import flash.events.EventDispatcher;import flash.events.IEventDispatcher;public class EventBus extends EventDispatcher{/** Application event bus instance */public static const instance:EventBus = new EventBus();/** * Normally the EventBus is not instantiated via the <b>new</b> method directly. * The constructor helps enforce only one EventBus available for the application * (singleton) so that it assures the communication only via a single event bus. */public function EventBus(){}/** * The factory method is used to create a instance of the EventBus. It returns * the only instance of EventBus and makes sure no another instance is created. */[Deprecated(replacement="instance")]public static function getInstance():EventBus{return instance;}/** * Basic dispatch function, dispatches simple named events. In the case * that the event is only significant by the event token (type string), * this new dispatch method simplify the code. */[Deprecated(replacement="AppEvent.dispatch")]public function dispatch(type:String):Boolean{return dispatchEvent(new Event(type));}}}

测试页面index.mxml

<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   xmlns:s="library://ns.adobe.com/flex/spark"   xmlns:mx="library://ns.adobe.com/flex/mx"   minWidth="955"   minHeight="600"   creationComplete="application1_creationCompleteHandler(event)"><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><fx:Script><![CDATA[import com.neil.AppEvent;import com.neil.titleWin;import mx.controls.Alert;import mx.events.FlexEvent;import mx.managers.PopUpManager;private var o:Object="abc";private function post(obj:Object):void{AppEvent.dispatch(AppEvent.TEST_EVENT, obj);var win:titleWin=new titleWin();PopUpManager.addPopUp(win, this, false);PopUpManager.centerPopUp(win);}protected function application1_creationCompleteHandler(event:FlexEvent):void{// TODO Auto-generated method stubAppEvent.addListener(AppEvent.TEST_EVENT, resultHandel)}private function resultHandel(event:AppEvent):void{Alert.show(event.data.toString());}]]></fx:Script><s:Button label="test"  click="post(o)"/></s:Application>

这里需要注意,只有在各模块中初始化事件监听后,所广播的事件才被接收,例如模块B初始化监听事件 

AppEvent.addListener(AppEvent.TEST_EVENT, resultHandel)

再执行模块A中的派发事件:

AppEvent.dispatch(AppEvent.TEST_EVENT, obj);

这样模块B中才能够接收到事件广播,否则不能。