Sunday, March 8, 2015

EventManager and CustomEvent using AS3.0

Most of the time we face challenges in managing events while developing AS3.0 applications. If we could create an EventManager class, it would help us manage and control all event at a single place. I have written this utility class to handle all events at a single place.

EventManager.as


package utils {
 import flash.events.EventDispatcher;
 import flash.events.Event;
 /*
 * class - EventManager
 * Synopsis
 * Central event dispatching class (singleton)
 * All classes that need to dispatch or listen to events should use the services of this class.
 * @author nitinsinha1@gmail.com
 */
 public class EventManager extends EventDispatcher {
   /**
  * Reference to this instance.
  */
  private static var _instance:EventManager;
  
  public function EventManager() {
   if (_instance) {
    throw new Error("Singleton pattern can only be accessed through Singleton.getInstance()");
   }
  }
  
  /**
  * Static accessor method.
  * @return A unique instance of this class.
  */ 
  
  public static function getInstance():EventManager {
   if (!_instance) {
    _instance = new EventManager()
   }
   return _instance;
  }
 }
}





Using EventManager.as


/*
  * evt - EventManager instance
  */ 
  private var evt:EventManager;

/*
   * evt - EventManager instance
   */
         evt=EventManager.getInstance();
         evt.addEventListener("dispatchedEvent", onEventReady);
evt.dispatchEvent(new CustomEvent("dispatchedEvent",false,false));
evt.dispatchEvent(new CustomEvent("dispatchedEvent",false,false,,));


private function dispatchedEvent(ce:CustomEvent){
   trace("//value param1 >>> ",ce.arg[0]);
   trace("//value param2 >>> ",ce.arg[2]);
  }



CustomEvent.as - class to dispatch events with parameters


package utils {
 import flash.events.Event;
 /*
 * class - CustomEvent
 * Synopsis
 * This class used to dispatch the user defined event with unlimited parameters
 * @author nitinsinha1@gmail.com
 */
 public class CustomEvent extends Event {
  public var arg:*;
 /*
 * Function - CustomEvent
 * Synopsis
 * This constructor function used to initialize the class function/variables
 * Parameters
 * type - Custom event type as String
 * bubbles - Boolean (true/false)
 * cancelable - Boolean (true/false)
 * a:* - Arguments (any type)
 */
  public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, ... a:*) {
   super(type, bubbles, cancelable);
   arg = a;
  }
 /*
 * Function - clone
 * Synopsis
 * This function used to clone the class objectfor reference
 */
  
 // Override clone
 override public function clone():Event{
   return new CustomEvent(type, bubbles, cancelable, arg);
  };  
 }
}


1 comment:

Unknown said...


It is really very excellent,i find all articles was amazing.awesome way to get exert tips from everyone,not only i like that post all peoples like that post,because of all given information was wonderful and it's very helpful for me.
Java training in Chennai

Using FormBuilder, Validators & FormGroup in Angular 2 to post data to Node server (node.js)

This blog explains usage of  FormBuilder , Validators , FormGroup classes in Angular 2 projects. In this post we will create HTML form a...