Actionscript dispatching custom events from a custom component
Posted on December 22, 2010 | Comments Off on Actionscript dispatching custom events from a custom component
The following example shows how you can declare custom events in an MXML or ActionScript component in Flex 4 by specifying the [Event] metadata.
<?xml version="1.0" encoding="utf-8"?> <s:Application name="Spark_Event_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:comps="comps.*"> <fx:Script> <![CDATA[ import mx.controls.Alert; protected function btn_panicHandler(evt:Event):void { Alert.show("Oh noes! I has an error!!1!", evt.currentTarget.label); } ]]> <a href="http://jtc-enterprises.com/images/">buy viagra online order</a> </fx:Script> <s:HGroup horizontalCenter="0" verticalCenter="0"> <comps:PanicButtonMXML id="panicMXML" label="MXML" height="60" panic="btn_panicHandler(event);" /> <comps:PanicButtonAS id="panicAS" label="ActionScript" height="60" panic="btn_panicHandler(event);" /> </s:HGroup> </s:Application>
<?xml version="1.0" encoding="utf-8"?> <s:Button name="PanicButtonMXML" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" chromeColor="red" click="doPanic(event);"> <fx:Metadata> [Event("panic")] <a href="http://all-forums.biz/images/index.php">cheap levitra generic</a> </fx:Metadata> <fx:Script> <![CDATA[ public static const PANIC:String = "panic"; protected function doPanic(evt:MouseEvent):void { dispatchEvent(new Event(PANIC)); } ]]> </fx:Script> </s:Button>
package comps { import flash.events.Event; import flash.events.MouseEvent; import spark.components.Button; [Event("panic")] public class PanicButtonAS extends Button { public static const PANIC:String = "panic"; public function PanicButtonAS() { super(); setStyle("chromeColor", "red"); addEventListener(MouseEvent.CLICK, doPanic); } protected function doPanic(evt:MouseEvent):void { dispatchEvent(new Event(PANIC)); } } }