Return to Snippet

Revision: 32821
at October 2, 2010 15:01 by bshantz


Updated Code
package utils  {
	import flash.events.Event;
	
	public class CustomEvent extends Event 
	{
		/*	Place as many events as you want to include in this class  */
		public static const EXAMPLE_EVENT:String = "exampleEvent";
		public static const ANOTHER_EVENT:String = "anotherEvent";
			
		/* 	Create properties to hold data that you want to pass with your event  
			Include as many as you like.  */
		public var dataObj:Object;
		
		public function CustomEvent(type:String, dataObj:Object = null) 
		{
			this.dataObj = dataObj;
			/*	Call the super function which fires off the event 
			set the event type, bubbling and cancelable properties  */
			super(type, false, false);
		}
		
		/*	Duplicates the instance of the event */
		override public function clone():Event
		{
			/*	This is the event that will be received by your handler function	*/
			return new CustomEvent(type, dataObj);
		}
	}
}

/********** To use this class would look something like this **************/

/*	Dispatch event with data to be passed
dataObj.name = "test data";
dispatchEvent(new CustomEvent(CustomEvent.EXAMPLE_EVENT, dataObj));

/*	 Listen for your custom event
addEventListener(CustomEvent.EXAMPLE_EVENT, handleExample);

/*	 Handle your custom event with custom data
function handleExample(event:CustomEvent):void {
var mydata:Object = event.dataObj;
}

/*************************************************************************/

Revision: 32820
at October 2, 2010 14:55 by bshantz


Initial Code
package utils  {
	import flash.events.Event;
	
	public class CustomEvent extends Event 
	{
		/*	Place as many events as you want to include in this class  */
		public static const EXAMPLE_EVENT:String = "exampleEvent";
		public static const ANOTHER_EVENT:String = "anotherEvent";
			
		/* 	Create properties to hold data that you want to pass with your event  
			Include as many as you like.  */
		public var dataObj:Object;
		
		public function CustomEvent(type:String, dataObj:Object = null) 
		{
			this.dataObj = dataObj;
			/*	Call the super function which fires off the event 
			set the event type, bubbling and cancelable properties  */
			super(type, false, false);
		}
		
		/*	Duplicates the instance of the event */
		override public function clone():Event
		{
			/*	This is the event that will be received by your handler function	*/
			return new CustomEvent(type, dataObj);
		}
	}
}

Initial URL


Initial Description
This is an event class that lets you package related events together and send custom data with the event, very useful for MVC applications.

Initial Title
Passing Data with Custom Events in AS3

Initial Tags
event

Initial Language
ActionScript 3