Flex 事件分发(FlexViewer事件机制)剥离过程

所属分类: 网络编程 / Flex 阅读数: 1997
收藏 0 赞 0 分享

将FlexViewer里面的事件分发及监听事件机制剥离出来在其他项目中使用

AppEvent.as

package com 
{ 
import flash.events.Event; 

/** 
* @author SamSung 
* 创建时间:2014-7-24 下午1:21:05 
* 
*/ 
public class AppEvent extends Event 
{ 
//-------------------------------------------------------------------------- 
// 
// Properties 
// 
//-------------------------------------------------------------------------- 

private var _data:Object; 

private var _callback:Function; 

public function AppEvent(type:String, data:Object = null, callback:Function = null) 
{ 
super(type); 
_data = data; 
_callback = callback; 
} 


/** 
* The data will be passed via the event. It allows the event dispatcher to publish 
* data to event listener(s). 
*/ 
public function get data():Object 
{ 
return _data; 
} 

/** 
* @private 
*/ 
public function set data(value:Object):void 
{ 
_data = value; 
} 

/** 
* The callback function associated with this event. 
*/ 
public function get callback():Function 
{ 
return _callback; 
} 

/** 
* @private 
*/ 
public function set callback(value:Function):void 
{ 
_callback = value; 
} 

/** 
* 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); 
} 

/** 
* Dispatch an AppEvent for specified type and with optional data and callback reference. 
*/ 
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 
{ 
import flash.events.Event; 
import flash.events.EventDispatcher; 

/** 
* The EventBus allows centrallized communication among modules without 
* point-to-point messaging. It uses the singleton design pattern 
* to make sure one event bus is available globally. The bus itself 
* is only available to the container. Modules use the container's 
* static method to communicate with the event bus. 
*/ 
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 EvenBus availiable for the application 
* (singeton) so that it asures the communication only via a sigle event bus. 
*/ 
public function EventBus() 
{ 
} 

/** 
* The factory method is used to create a instance of the EventBus. It returns 
* the only instanace 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)); 
} 
} 
}
更多精彩内容其他人还在看

Flex父子窗口相互调用实现思路及源码

这篇文章主要介绍了Flex父子窗口相互调用实现思路及源码,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex中如何判断是否在组件之外单击

这篇文章主要介绍了在Flex中判断是否在组件之外单击的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex中TitleWindow传值思路及实现

这篇文章主要介绍了Flex中TitleWindow传值思路及实现,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex DataGrid自动编号示例

这篇文章主要介绍了Flex DataGrid如何自动编号,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

Flex DataGrid 伪合并单元格思路及代码

这篇文章主要介绍了Flex DataGrid 伪合并单元格思路及代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Flex读取txt文件中的内容报错原因分析及解决

这篇文章主要介绍了Flex读取txt文件中的内容报错原因分析及解决,需要的朋友可以参考下
收藏 0 赞 0 分享

FLEX ArrayCollection删除过滤的数据问题解决

ArrayCollection添加过滤器后,调用removeItemAt()是无法删除的,下面有个不错的解决方法,大家可以参考下
收藏 0 赞 0 分享

Flex 错误(mx.messaging.messages::RemotingMessage)分析

有时我们在做项目的时候会遇到Flex 错误提示mx.messaging.messages::RemotingMessage,那么产生这个错误的原因是什么呢,今天我们来分析下
收藏 0 赞 0 分享

手把手教你使用flex eclipse整合spring

Flex 是一个高效、免费的开源框架,可用于构建具有表现力的 Web应用程序,这些应用程序利用Adobe Flash Player和Adobe AIR, 可以实现跨浏览器、桌面和操作系统。我们今天就来好好研究下他
收藏 0 赞 0 分享

flex小技巧之加载GIF图片

这篇文章主要是flex使用GIFPlayer加载GIF图片的代码示例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多