当前位置:

首页 > 编程开发 > 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应用程序中的作用

    Univer 1.6.2怎样选择取值接口并核对升级后的保护行为?
    Univer 1.6.2怎样选择取值接口并核对升级后的保护行为?

    Univer 1.6.2有效值、显示值与原始值接口的区别,含区域读取代码、数据类型检查及保护与Pro协作权限的升级要点。

    domain-modeling 2b7ab8c9471fad3fca0a9c0c9e1f52153408172b:如何校对词汇定义?
    domain-modeling 2b7ab8c9471fad3fca0a9c0c9e1f52153408172b:如何校对词汇定义?

    Matt Pocock skills 在2026年11月25日提交2b7ab8c9471fad3fca0a9c0c9e1f52153408172b中要求加粗定义引用的术语并说明关系。通过订单、发票与客户示例,按顺序整理名称、定义及关联。

    GitHub Copilot CLI 1.0.116-5预发布版有哪些审批与回退修复?
    GitHub Copilot CLI 1.0.116-5预发布版有哪些审批与回退修复?

    GitHub Copilot CLI 1.0.116-5预发布版显示实际审批命令,改进钩子、回退提示和窄终端显示;通过官方API核对历史版本及修复范围。

    Chrome DevTools MCP 1.14.0后台开页和网络模拟怎么设置
    Chrome DevTools MCP 1.14.0后台开页和网络模拟怎么设置

    Chrome DevTools MCP 1.14.0支持后台打开测试页与中止指定请求。介绍固定版本的MCP配置、实际页面编号、网络规则移除,以及来源控制和工作线程的使用边界。

    GitHub MCP Server 2.3.0怎么用好安全告警更新能力?
    GitHub MCP Server 2.3.0怎么用好安全告警更新能力?

    GitHub MCP Server 2.3.0可更新依赖、代码扫描和密钥扫描告警,介绍只读起步、工具配置与关键参数限制。

    Ducter v0.1.97 值得关注哪些 Cursor 与任务管理变化?
    Ducter v0.1.97 值得关注哪些 Cursor 与任务管理变化?

    解释 Cursor 接入、等结果计数与 Keep 归属调整,给出 Linux 发行包安装顺序和搬家边界。

    llama.cpp b12809的预发布更新适合哪些Vulkan任务
    llama.cpp b12809的预发布更新适合哪些Vulkan任务

    llama.cpp b12809为Inkling 2 Mini加入Vulkan紧凑偏置Flash Attention支持,并修复相关路径问题;使用前先确认预发布渠道和实际任务范围。

    llama.cpp b12808的反量化精度变化该怎样看?
    llama.cpp b12808的反量化精度变化该怎样看?

    llama.cpp b12808修复SYCL后端重排Q6_K反量化的精度一致性问题。通过发行记录和已有oneAPI设备检查,判断适用范围,理解预发行与官方测试的边界。

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

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

    Windows
    Windows

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

    PDF教程
    PDF教程

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

    Mac软件 更多
    汽水音乐
    汽水音乐
    Android / iOS / Windows / macOS;按官网选择下载

    汽水音乐提供个性化推荐、场景电台及抖音音乐收藏衔接,适合发现歌曲和管理日常听歌内容。这里说明播放、一起听及多平台入口,并提醒区分曲目版权与会员权益。

    LibreOffice 桌面版
    LibreOffice 桌面版
    Windows / macOS / Linux 桌面版;按官网选择稳定分支和架构

    LibreOffice桌面版提供Writer、Calc、Impress等办公组件,可本地处理文档并交换Office格式。了解实际功能、商用许可、兼容性检查和企业支持的区别。

    Numbers macOS版
    Numbers macOS版
    macOS版;系统要求以 Mac App Store 当前页面为准

    Numbers macOS版用于预算、数据整理和可视化报告,支持公式、图表、数据透视表及iCloud协作。了解Excel文件交换、Mac安装条件和多端使用差异。

    WINDOWS 更多
    汽水音乐
    汽水音乐
    Android / iOS / Windows / macOS;按官网选择下载

    汽水音乐提供个性化推荐、场景电台及抖音音乐收藏衔接,适合发现歌曲和管理日常听歌内容。这里说明播放、一起听及多平台入口,并提醒区分曲目版权与会员权益。

    微信 Windows版
    微信 Windows版
    Windows版;按官网选择架构和适用系统,不固定历史小版本

    微信Windows版适合电脑聊天、会话查找和文件传送。本页说明登录与文件管理、电脑办公优势,以及大文件失败、版本差异和本地记录的核对方法。

    LibreOffice 桌面版
    LibreOffice 桌面版
    Windows / macOS / Linux 桌面版;按官网选择稳定分支和架构

    LibreOffice桌面版提供Writer、Calc、Impress等办公组件,可本地处理文档并交换Office格式。了解实际功能、商用许可、兼容性检查和企业支持的区别。