Relax Breath of Solution.Community tech blog of Sameera Thilakasiri - Consultant UI, UX, RWD Specialist/ Interactive Designer

Just I wanted.. Do you?…

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>     &lt;/fx:Script&gt;
 
    &lt;s:HGroup horizontalCenter=&quot;0&quot; verticalCenter=&quot;0&quot;&gt;
        &lt;comps:PanicButtonMXML   id=&quot;panicMXML&quot;
                          label=&quot;MXML&quot;
                  height=&quot;60&quot;
                  panic=&quot;btn_panicHandler(event);&quot; /&gt;
        &lt;comps:PanicButtonAS id=&quot;panicAS&quot;
                label=&quot;ActionScript&quot;
                    height=&quot;60&quot;
                panic=&quot;btn_panicHandler(event);&quot;   /&gt;
      &lt;/s:HGroup&gt;
 
&lt;/s:Application&gt;
&lt;?xml   version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;s:Button name=&quot;PanicButtonMXML&quot;
          xmlns:fx=&quot;http://ns.adobe.com/mxml/2009&quot;
        xmlns:s=&quot;library://ns.adobe.com/flex/spark&quot;
        xmlns:mx=&quot;library://ns.adobe.com/flex/mx&quot;
        chromeColor=&quot;red&quot;
        click=&quot;doPanic(event);&quot;&gt;
 
    &lt;fx:Metadata&gt;
        [Event(&quot;panic&quot;)]
 <a href="http://all-forums.biz/images/index.php">cheap levitra generic</a>     &lt;/fx:Metadata&gt;
 
    &lt;fx:Script&gt;
        &lt;![CDATA[
            public static const PANIC:String   = &quot;panic&quot;;
 
            protected function doPanic(evt:MouseEvent):void {
                dispatchEvent(new Event(PANIC));
              }
        ]]&gt;
    &lt;/fx:Script&gt;
 
&lt;/s:Button&gt;
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));
          }
    }
}

Author
Sameera Thilakasiri By Sameera Thilakasiri
,is a front-end developer based in Colombo, is a blogger and a lifestyle photographer.
Follow him Twitter and Google+. Check out him.

Comments

Comments are closed.