CustomEvent
This class allows you to extend the Event class in order to be able to pass values to the event listener.I have used this class in just about every project I work on. It even works well with PureMVC projects as well. Hope this helps.
CustomEvent(type:String, params:Object = null, bubbles:Boolean = false, cancelable:Boolean =false)
Creates an Event object to pass as a parameter to event listeners with the included ability to pass additional parameters.
- Class version:
- 1.0
- Last Updated:
- July 3, 2009
- Requires:
- Actionscript 3
- Download:
- http://freerksen.googlecode.com/files/flex_customevent.zip
Source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.witheringtree.controller.events { import flash.events.Event; /** * Extension of the Event class to allow you to pass parameters to the event handlers. */ public class CustomEvent extends Event { // Add custom event constants below public static const ACTION:String = "action"; public var params:Object; /** * Create a new event. */ public function CustomEvent(type:String, params:Object=null, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); this.params = params; } /** * Clone an created event. * @return Cloned event. */ public override function clone():Event { return new CustomEvent(type, this.params, bubbles, cancelable); } /** * Return event as a string. * @return Returns string. */ public override function toString():String { return formatToString("CustomEvent", "params", "type", "bubbles", "cancelable"); } } } |
Usage:
1 2 3 4 5 6 | myObject.addEventListener(CustomEvent.ACTION, doEvent); public function doEvent(evt:CustomEvent):void { Alert.show("TYPE: " + evt.type + "\nTARGET: " + evt.target + "\nFIRST CUSTOM PARAM: " + evt.params.param1 + "\nSECOND CUSTOM PARAM: " + evt.params.param2); } dispatchEvent(new CustomEvent(CustomEvent.ACTION, {param1:"first param", param2:"second param"})); |
There are no comments