Flex: How to create a custom Event

1423 ワード

1. Create a subclass that extends the flash.events.Event base class
2. Add properties to the subclass to hold the data you want to dispatch
3. Create the event class's constructor:
        3.1 Invoke the super() method of the Event class within the constructor, passing the event type to it
        3.2 Populate the class properties from the constructor arguments
4. Override the clone() method of the event class with the new subclass.
 
Attention:
The constructor should do at least 4 things:
         1. Accept an argument for the event type
         2. Accept arguments for the data you want to pass
         3. Call the super() method
         4. Populate the class properties from the arguments
 
Eg.
 
package event
{
	import flash.events.Event;
	
	import valueObjects.Employee;

	public class ShowPreview extends Event
	{
		public var employee:Employee;	//Data for transfer
		public var message:String;		//Data for transfer
		
		public function ShowPreview(type:String, employee:Employee, message:String)
		{
			super(type);				//type info: can be Event.CLICK or custom event type defined in <metadata>
			
			this.employee = employee;
			this.message = message;
		}
	}
}