3

这不是生死攸关的问题,但我想知道这是否可能:

我从一种类型的自定义事件 (FormEvent) 中得到了几个事件,现在我得到了一个 FormListener,它监听所有这些事件并根据事件类型处理它们。我希望一次添加所有事件,而不是一次添加一个 eventListener。

所以现在看起来像这样:

        private function addListeners():void {

        addEventListener(FormEvent.SHOW_FORM, formListener);
        addEventListener(FormEvent.SEND_FORM, formListener);
        addEventListener(FormEvent.CANCEL_FORM, formListener);
    }



        private function formListener(event:formEvent):void {

        switch(event.type){
            case "show.form": 
                // handle show form stuff
            break;
            case "send.form":
                // handle send form stuff
            break;
            case "cancel.form":
                // handle cancel form stuff
            break;
        }
    }

但不是每次都添加一个事件,我宁愿做类似的事情

    private function addListeners():void {

        addEventListener(FormEvent.*, formListener);
    }

我想知道这样的事情是否可能,我会喜欢的。我处理大量事件:)

4

2 回答 2

7

无论如何,在这种情况下,您实际上只需要一个事件侦听器。该侦听器将侦听表单的任何更改,并且与更改内容相等的参数可用于事件侦听器函数。我将向您展示,但请记住,这是一种伪情况,通常我不会从方法调用这样简单的事情中分派事件,因为分派是隐含的,因此没有真正需要监听它。

首先是自定义事件


package com.yourDomain.events
{
    import flash.events.Event;
    public class FormEvent extends Event
    {
        //Public Properties
        public static const CANCEL_FORM:int = "0";
        public static const SHOW_FORM:int = "1";
        public static const SEND_FORM:int = "2";

        public static const STATE_CHANGED:String = "stateChanged";

        //Private Properties
        private var formState:int;

        public function FormEvent(formState:int):void
        {
            super(STATE_CHANGED);
            formState = formState;
        }
    }
}

因此,我们刚刚创建了自定义事件类,并且我们已经对其进行了设置,以便我们可以通过侦听器函数捕获状态,正如我将演示一旦完成将调度所述自定义事件的伪表单类。

请记住,这都是假设的,因为我不知道您的代码是什么样子或您如何实现的。重要的是要注意,当我调度事件时,我需要发送一个反映新状态的参数。


package com.yourDomain.ui
{
   import flash.events.Event;
   import flash.events.EventDispatcher;
   import com.yourDomain.events.FormEvent;
   public class Form extends EventDispatcher
   {
     public function Form():void
     {
        //Anything you want form to do upon instantiation goes here.
     }
     public function cancelForm():void
     {
        dispatchEvent(new Event(FormEvent.CANCEL_FORM);
     }
     public function showForm():void
     {
        dispatchEvent(new Event(FormEvent.SHOW_FORM);
     }
     public function sendForm():void
     {
        dispatchEvent(new Event(FormEvent.SEND_FORM);
     }
   }
}

最后,我们创建将监听它的文档类。请知道,我意识到创建一个在调用类的方法时触发的侦听器是不合逻辑的,因为您显然知道您调用了该方法,但对于这个示例,它会到期。


package com.yourDomain.ui
{
   import com.yourDomain.ui.Form;
   import com.yourDomain.events.FormEvent;
   //Form is in the same package so we need not import it.
   public class MainDocumentClass
   {
      private var _theForm:Form;

      public function MainDocumentClass():void
      {
         _theForm  = new Form();
         _theForm.addEventListener(FormEvent.STATE_CHANGED, onFormStateChange, false, 0, true);
         /*
         The following three method calls each cause the
         FormEvent.STATE_CHANGE event to be dispatched.  
         onFormStateChange is notified and checks what 
         the last change actually was.
         */
         _theForm.cancelForm();
         _theForm.showForm();
         _theForm.sendForm();
      }
      private function onFormStateChange(e:FormEvent):void
      {
         switch(e.formState)
         {
            case CANCEL_FORM:
              trace('The form was canceled');
              break;
            case SHOW_FORM:
              trace('The form was revealed');
              break;
            case SEND_FORM:
              trace('The form was sent');
              break;
         }
      }
   }      
}

我希望这是有帮助的,它已经晚了,我可能需要稍后修改一些东西,但这应该有助于了解如何制作自己的事件并自定义事情的工作方式。

于 2008-09-26T02:32:35.630 回答
0

我不知道有任何例程可以让您直接执行此操作,但您可以编写自己的例程。这里的语法并不完美,但这是第一遍:

private function addMultipleEventListeners( evts:Array, callback:function ):void
{
    for each( var evt:Event in evts )
    {
        addEventListener( evt, callback );
    }
}

然后,您可以像这样调用该例程:

var evts:Array = [ FormEvent.SHOW_FORM, FormEvent.SEND_FORM, FormEvent.CANCEL_FORM ];
addMultipleEventListeners( evts, formListener );
于 2008-09-25T21:27:39.577 回答