Custom Event Handler named add will fire when the compenent is added to stage.

In the example below, I've created a component named two.mxml and it's based on the Panel component. Inside the two.mxml component meta data defines an event called "add". Here is the problem, the Panel componenet already has a method called "add". It's fired when the component is added to the stage.

 

 

 

 

<?xml version="1.0" encoding="utf-8"?>
    <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml"
        title="Two"
        paddingBottom="10"
        paddingLeft="10"
        paddingRight="10"
        paddingTop="10">
        <mx:Script>
            <![CDATA[
            import com.TwoEvent;
            import flash.events.Event;
            public function addHandler(event:MouseEvent):void
            {
                var submitEvent:TwoEvent = new TwoEvent(TwoEvent.ADD);
                dispatchEvent(submitEvent);
            }
            ]]>
        </mx:Script>

        <mx:Metadata>
                [Event(name="add", type="flash.events.Event")]
        </mx:Metadata>

        <mx:Button label="Dispatch and Say Hello" click="addHandler(event)"/>
</mx:Panel>

 

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="absolute" xmlns:local="*"
  viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            public function changeState():void
            {
                currentState='two';
             }

             public function SayHello():void
            {
                Alert.show("Hello Panel Two");
             }


        ]]>
</mx:Script>

<mx:states>
        <mx:State name="two">
                <mx:AddChild position="lastChild">
                    <local:two add="SayHello()" x="254.5" y="20" width="222" height="155" horizontalAlign="center" verticalAlign="middle"/>
                </mx:AddChild>
            </mx:State>
</mx:states>

      <mx:Panel title="One" width="195" height="155" x="28" y="20" id="panel1" horizontalAlign="center" verticalAlign="middle">
            <mx:Button label="Show Panel 2" click="changeState();" />
      </mx:Panel>
</mx:Application>

 

 

The easy solution is to rename the custom event in your two.mxml componenet from "add" to something like "addNew". This was something I stumbled over and hope this helps anyone who's made this same mistake.

 

 

0 responses to “Custom Event Handler named add will fire when the compenent is added to stage.”

Leave a Reply

Leave this field empty:

Powered by Mango Blog.