/ Published in: ActionScript 3

This is an event class that lets you package related events together and send custom data with the event, very useful for MVC applications.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; } /*************************************************************************/
Comments
