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

所属分类: 网络编程 / Flex 阅读数: 1980
收藏 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的tree动态加载大量数据与滚动条相关问题探讨

本文将对flex的tree动态加载大量数据与滚动条相关的问题进行探讨,感兴趣的朋友可以参考下哈,希望对你有所帮助
收藏 0 赞 0 分享

flex tree自动显示横向滚动条实现代码

flex tree自动显示横向滚动条想必有很多的朋友都不会吧,下面与大家分享下具体的实现方法,感兴趣的你可不要错过了哈
收藏 0 赞 0 分享

在as中监听自定义事件并处理事件的实例代码

点击一张图片,响应事件。必须在AS中,去监听事件,并处理事件,下面是具体的实现思路及功能代码,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

FLEX HashMap遍历并取到需要的值

在项目用到了HashMap,需要遍历,并取到需要的值,具体实现代码如下,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

如何在Renderer中设置属性 Renderer中设置属性的方法实例

如何在Renderer中设置属性 Renderer中设置属性的方法实例,需要的朋友可以参考一下
收藏 0 赞 0 分享

flex复选框和下拉列表的几种用法整理

自己闲暇时间整理了有关复选框可下拉的几种用法,在这里与大家分享下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
收藏 0 赞 0 分享

Flex中实现对一个text渲染不同的字体颜色示例

本文为大家详细介绍下Flex中如何实现对一个text渲染不同的字体颜色,具体的实现思路及代码如下,有兴趣的朋友可以参考下哈,希望对大家有所帮助
收藏 0 赞 0 分享

Flex正则表达式判断中文或全角字符代码

文本框值的length取出的是字符个数,并不是字节长度,如果含有中文或者全角字符,一个字符是2个字节,下面与大家分享下具体的判断方法,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

Flex 改变树结点图标的2种方法介绍

本文为大家介绍两种方法改变树结点图标:根据是否有子结点进行改变、根据结点的属性,灵活改变图标,具体实现如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
收藏 0 赞 0 分享
查看更多