• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

将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)); 
} 
} 
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Flex AIR 重启需要修改的配置文件发布时间:2022-02-05
下一篇:
Flex 获取每月第几周小例子发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap