如何实现自定义事件? [英] How to implement Custom Events?

查看:110
本文介绍了如何实现自定义事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何正确地实现自定义事件?我认为以下应工作,但我从来没有收到CustomEvent.READY的主力机型

 包MVC
{
    进口对象类型:flash.events.Event;
    公共动态类自定义事件扩展事件
    {
       公共静态常量MY_EVENT:字符串=myEvent;
       公共静态常量READY:字符串=准备好了;

    公共功能自定义事件(类型:字符串)
    {
        超级(类型);
    }
}
}
 

在Model.as延伸AbstractModel它扩展了EventDispatcher

 私有函数initWorld():无效{
        _worldModel =新WorldModel();
        _worldModel.addEventListener(CustomEvent.READY,更新);
}
 

然后在WorldModel.as延伸AbstractModel它扩展了EventDispatcher,我派遣一个事件,但更新不会被调用。为什么呢?

 则dispatchEvent(新的事件(CustomEvent.READY));
 

解决方案

  _worldModel.dispatchEvent(新自定义事件(CustomEvent.READY));
 

您必须实例化一个自定义事件,而不是一个事件。很大的区别。

您也可以使用自定义事件传递其他参数与调度的事件,如果你使用你的自定义事件的严重,这将证明是令人惊讶的方便

 包com.b99.events
{
    进口对象类型:flash.events.Event;

    / **
     * ...
     * @author bosworth99
     * /
    公共类AppEvents扩展事件
    {
        公共静态常量APP_READY:字符串=应用就绪;
        公共静态常量XML_LOADED:字符串=XML已经装;
        公共静态常量CHANGE_COMPLETE:字符串=状态变化完成;
        公共静态常量PAGE_ADDED:字符串=页面的内容增加了;
        公共静态常量PAGE_REMOVED:字符串=页面内容删除;
        公共静态常量LIBRARY_LOADED:字符串=外部库加载;
        公共静态常量IMAGE_LOADED:字符串=外在形象装;
        公共静态常量LOAD_ERROR:字符串=外部加载失败;

        公共变种ARG:*;

        公共职能AppEvents(类型:字符串,气泡:布尔=假,取消:布尔=假,答:*)
        {
            超(类型,泡沫,取消);
            ARG =一个;
        }

        重写公共职能的clone():事件
        {
            返回新AppEvents(类型,泡沫,取消,ARG);
        }

    }
}
 

您可以再传给任意数量的参数,并到接收功能:

  this.dispatchEvent(新AppEvents(AppEvents.LIBRARY_LOADED,假的,假的,_name,_library,_names));
 

和访问他们在recieving函数作为数组。

 私有函数onLibraryLoad(E:AppEvents):无效
        {
            _digestExternalLib.removeEventListener(AppEvents.LIBRARY_LOADED,onLibraryLoad);

            变种CURRENTINDEX:整数= AppData.navLocations.indexOf(e.arg [0],0);

            AppData.libraries.push(e.arg [0]);
            AppData.libraryCon.push(e.arg [1]);
            AppData.libraryNames.push(e.arg [2]);

        }
 

我猛拉这从一个正常运作的项目。但你应该能够收集重要的位... 祝你好运!

How do I correctly implement custom events? I thought the following should work, but I never receive CustomEvent.READY in the main Model

package mvc
{
    import flash.events.Event;
    public dynamic class CustomEvent extends Event
    {
       public static const MY_EVENT:String = "myEvent";
       public static const READY:String = "ready";

    public function CustomEvent(type:String)
    {
        super(type);
    }
}
}

In the Model.as which extends AbstractModel which extends EventDispatcher

private function initWorld():void {
        _worldModel = new WorldModel();
        _worldModel.addEventListener(CustomEvent.READY, update);
}

Then in WorldModel.as which extends AbstractModel which extends EventDispatcher, I dispatch an event, but update is never called. why?

dispatchEvent(new Event(CustomEvent.READY));

解决方案

_worldModel.dispatchEvent(new CustomEvent(CustomEvent.READY));

You must instantiate a CustomEvent, not an Event. Big difference.

You could also use your custom event to pass additional parameters with the dispatched event, which will prove amazingly handy if you make use of your CustomEvent heavily

package com.b99.events 
{
    import flash.events.Event;

    /**
     * ...
     * @author bosworth99
     */
    public class AppEvents extends Event
    {
        public static const APP_READY       :String = "application ready";
        public static const XML_LOADED      :String = "XML has loaded";
        public static const CHANGE_COMPLETE :String = "state change complete";
        public static const PAGE_ADDED      :String = "page content added";
        public static const PAGE_REMOVED    :String = "page content removed";
        public static const LIBRARY_LOADED  :String = "external library loaded";
        public static const IMAGE_LOADED    :String = "external image loaded";
        public static const LOAD_ERROR      :String = "external load failed";

        public var arg:*;

        public function AppEvents(type:String, bubbles:Boolean = false, cancelable:Boolean = false, ...a:*) 
        {
            super(type, bubbles, cancelable);
            arg = a;
        }

        override public function clone():Event 
        {
            return new AppEvents(type, bubbles, cancelable, arg);
        }

    }
}

You can then pass any number of arguments along to a receiving function:

this.dispatchEvent(new AppEvents(AppEvents.LIBRARY_LOADED, false , false, _name, _library, _names));

And access them in the recieving function as an array.

private function onLibraryLoad(e:AppEvents):void 
        {
            _digestExternalLib.removeEventListener(AppEvents.LIBRARY_LOADED, onLibraryLoad);

            var currentIndex:int = AppData.navLocations.indexOf(e.arg[0], 0);

            AppData.libraries.push(e.arg[0]);
            AppData.libraryCon.push(e.arg[1]);
            AppData.libraryNames.push(e.arg[2]);

        }

I yanked this from a functioning project... but you should be able to gather the important bits... Good luck!

这篇关于如何实现自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆