加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

创建Flex事件总线AppEvent

发布时间:2020-12-15 04:02:09 所属栏目:百科 来源:网络整理
导读:为了使程序各模块间达到 高内聚低耦合 ,各模块间的事件传递一般采用事件总线方式,即将要传递的事件派发到事件总线进行广播,然后在需要接收事件的模块中进行监听,这样就能使模块结构更加清晰,从代码结构上看,也更加符合软件设计标准。 创建事件总线控制

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

创建事件总线控制类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,useCapture:Boolean = false):void
		{
			EventBus.instance.removeEventListener(type,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 stub
				AppEvent.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中才能够接收到事件广播,否则不能。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读