views:

1420

answers:

2

It's not a matter of life or death but I wonder if this could be possible:

I got a couple of events from one type of custom event (FormEvent) now I got a FormListener that listens to all those events and handles them according to the event type. Instead of adding one eventListener at the time I wish to add all events at once.

so 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 every event one at the time I would rather be doing something like

 private function addListeners():void {

  addEventListener(FormEvent.*, formListener);
 }

I wonder if something like this is possible, i would love it. I work with loads of events :)

A: 

I don't know of any routines that let you do that directly, but you could write your own. The syntax here won't be perfect, but here's a first pass:

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

You could then call that routine like so:

var evts:Array = [ FormEvent.SHOW_FORM, FormEvent.SEND_FORM, FormEvent.CANCEL_FORM ];
addMultipleEventListeners( evts, formListener );
Matt Dillard
+5  A: 

You only really need one event listener in this case anyhow. That listener will be listening for any change with the form and a parameter equal to what the change was becomes available to the event listener function. I will show you, but please remember that this is a pseudo situation and normally I wouldn't dispatch an event off of something as simple as a method call because the dispatch is implied so there is no real need to listen for it.

First the Custom Event


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

So we have just created our custom event class and we have set it up so that we can catch the state through the listener function as I will demonstrate once done with the pseudo form class that will dispatch the for said custom event.

Remember that this is all hypothetical as I have no idea what your code looks like or how your implementing things. What is important is to notice that when I dispatch the event I need to send a parameter with it that reflects what the new state is.


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

And finally we create the document class that will listen for it. Please know that I realize it isn't logical to create a listener that fires when you call a method of a class because you obviously know you called the method, but for this example it will due.


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

I hope that this was helpful, its late and I may have to revise some things later, but this should help get an understanding of how to make your own events and to customize how things work

For the best tutorials check out Lee Brimelow http://www.gotoandlearn.com and http://theflashblog.com. ENJOY!

<3AS3

Brian Hodge