发布于2026-07-22 阅读(0)
扫一扫,手机访问
在FlexViewer项目中,事件分发和监听机制是模块间解耦的关键。很多开发者会想:能不能把这套机制抽出来,直接用在其他项目里?答案是肯定的,而且实现起来非常简洁。
核心思路是定义一个自定义事件类 AppEvent,以及一个全局的单例事件总线 EventBus。先看事件类:
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);
}
}
}
这个类除了继承 flash.events.Event 的基本功能外,还额外携带了 data 和 callback 两个属性。这样,事件不仅传递消息,还能附带数据,甚至在事件处理完成后执行回调——这在异步流程中非常实用。另外,它还提供了静态的 dispatch、addListener、removeListener 方法,直接袋里到全局的 EventBus,省去了手动获取单例的麻烦。
接下来是事件总线本身:
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 a vailable globally. The bus itself
* is only a vailable 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 new method directly.
* The constructor helps enforce only one EvenBus a vailiable 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));
}
}
}
EventBus 继承自 EventDispatcher,采用单例模式,整个应用只有一个全局实例。所有模块通过 AppEvent 的静态方法间接与这个总线通信,避免了模块之间的直接耦合。值得注意的是,代码中保留了 getInstance() 和 dispatch(type) 两个废弃方法,它们是为了向后兼容而存在的,实际使用中推荐直接通过 AppEvent.dispatch 和 EventBus.instance 来操作。
这套剥离出来的事件机制,完全可以独立于 FlexViewer 使用。你只需要把这两个类复制到你的项目里,然后就能享受到全局事件通信的便利了。无论是模块间解耦,还是异步回调处理,都变得干净利落。
下一篇:Flex 获取每月第几周小例子
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8