当前位置:

首页 > 编程开发 > Flex 事件分发(FlexViewer事件机制)剥离过程

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

在FlexViewer项目中,通过定义AppEvent自定义事件类与单例EventBus事件总线,实现模块间解耦。AppEvent携带数据与回调,通过静态方法代理至EventBus,支持全局事件通信,且该机制可独立复用。

在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 使用。你只需要把这两个类复制到你的项目里,然后就能享受到全局事件通信的便利了。无论是模块间解耦,还是异步回调处理,都变得干净利落。

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
作者最新文章
编程开发
相关文章 更多
C#事件机制详解与使用方法
C#事件机制详解与使用方法

C#的事件机制是一种基于委托的对象间通信方式,旨在实现发布者与订阅者之间的解耦。其核心组成部分包括:1.委托(定义事件处理方法的签名);2.事件(封装委托并控制订阅过程);3.发布者(触发事件的对象);4.订阅者(响应事件的对象)。通过事件机制,发布者无需了解订阅者的存在或处理逻辑,从而提升代码的可维护性和安全性。相比直接使用委托,事件限制了外部随意触发行为,并提供了add/remove访问器以增强控制力。C#事件机制本质上是观察者模式的实现,适用于UI交互、数据绑定、异步通知及游戏开发等场景。为避免内存

掌握ThinkPHP6的事件机制
掌握ThinkPHP6的事件机制

随着Web应用程序规模不断扩大,如何更好地处理事件已成为我们开发的关键。ThinkPHP6提供了事件机制,可以帮助我们更好地处理Web应用程序中的事件。事件机制在Web应用程序中的作用事件机制是一种应用程序设计模式,它涉及将应用程序设计为由事件驱动的系统。具体而言,事件是一种“触发器”,当事件发生时,关联的代码将被激活并执行。事件机制在Web应用程序中的作用

查看更多
精品专题 更多
装机必备
装机必备

正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

Windows
Windows

正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

PDF教程
PDF教程

正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

Mac软件 更多
Postman macOS版
Postman macOS版
Mac

Postman是一套面向API全生命周期的开发与测试平台,可在Mac上发送和调试多种协议请求,管理集合、环境及认证信息,并完成脚本测试、模拟服务、监控、文档发布和团队协作。它适合开发、测试及运维人员使用,提供个人免费方案。

Docker Desktop macOS版
Docker Desktop macOS版
Mac

Docker Desktop 是 Docker 官方为 Mac 提供的容器开发环境,集成 Docker Engine、命令行工具、Compose、Build、Kubernetes 与图形化管理界面。

Android Studio macOS版
Android Studio macOS版
Mac

Android Studio 是 Google 面向安卓应用开发提供的官方集成开发环境,基于 IntelliJ IDEA 构建。它整合智能代码编辑、Gradle 构建、界面预览、设备模拟、性能分析、调试测试及 APK 分析等工具。

WINDOWS 更多
Postman Windows版
Postman Windows版
Windows

Postman 是面向 API 全生命周期的开发与协作平台。Windows 桌面版可用于构建和发送 HTTP、GraphQL、gRPC、WebSocket、MQTT 等请求,并提供环境变量、集合、脚本测试、模拟服务、监控、文档及团队工作区等

Docker Desktop Windows版
Docker Desktop Windows版
Windows

Docker Desktop 为 Windows 开发者提供集成式容器环境,内置 Docker Engine、命令行工具、Compose、Build 与 Kubernetes 等组件,可在本机完成镜像构建、容器运行、日志查看和资源管理。

Android Studio Windows版
Android Studio Windows版
Windows

Android Studio 是 Google 面向 Android 应用开发提供的官方集成开发环境,以 IntelliJ IDEA 为基础,集成智能代码编辑、Gradle 构建、设备模拟、界面预览、调试测试及性能分析工具,