Flash: listen to all events with a single event type

This is not a matter of life or death, but I wonder if this is possible:

I got a couple of events from one type of custom event (FormEvent), now I have a FormListener that listens for all these events and processes them according to the type of event. Instead of adding one eventListener at a time when I want to add all events at once.

now it looks like this:

        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;
        }
    }

but instead of adding each event at a time when I prefer to do something like

    private function addListeners():void {

        addEventListener(FormEvent.*, formListener);
    }

I wonder if something like this is possible, I would like it. I work with a lot of events :)

+3
source share
2 answers

. , , . , , , , -, - , , , .


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;
         }
      }
   }      
}

, , , , , , , , .

+7

, , . , :

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 );
0

Source: https://habr.com/ru/post/1697539/


All Articles