4 Replies Latest reply on May 24, 2010 1:07 AM by tweek214

    Custom component and custom event problem

    tweek214

      hello , i have a strange problem since one week , is that i can't handle the events becoming from my custom component , here is my code :

       

      Event Class :

      package events

      {

           import flash.events.Event;

           

           public class Ev extends Event

           {

                public static const UPDATE:String="update";

                public var data:String;

                public function Ev(type:String,data:String)

                {

                     super(type);

                     this.data=data;

                }

                override public function clone():Event

                {

                     return new Ev(type,data);

                }

           }

      }

       

      MXML component :

      <?xml version="1.0" encoding="utf-8"?>

      <mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"

                 xmlns:s="library://ns.adobe.com/flex/spark"

                 xmlns:mx="library://ns.adobe.com/flex/mx">

           <fx:Metadata>

                [Event(name="onMenuChange",type="events.Ev")]

           </fx:Metadata>

           <fx:Script>

                <![CDATA[

                     import events.Ev;

                     

                     import mx.events.IndexChangedEvent;

                     

                     protected function accordion1_changeHandler(event:IndexChangedEvent):void

                     {

                          dispatchEvent(new Ev(Ev.UPDATE,event.newIndex.toString()));

                     }

                     

                ]]>

           </fx:Script>

           <fx:Declarations>

                <!-- Placer ici les éléments non visuels (services et objets de valeur, par exemple). -->

           </fx:Declarations>

           <mx:Accordion width="200" height="200" change="accordion1_changeHandler(event)">

                <s:NavigatorContent label="Menu 1" width="100%" height="100%">

                </s:NavigatorContent>

                <s:NavigatorContent label="Menu 2" width="100%" height="100%">

                </s:NavigatorContent>

                <s:NavigatorContent label="Menu 3" width="100%" height="100%">

                </s:NavigatorContent>

           </mx:Accordion>

      </mx:VBox>

       

      the main container :

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                        xmlns:s="library://ns.adobe.com/flex/spark"

                        xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*">

           <fx:Script>

                <![CDATA[

                     import events.Ev;

                     

                     import mx.controls.Alert;

                     

                     protected function comp1_onMenuChangeHandler(event:Ev):void

                     {

                          Alert.show(event.data,"notice");

                     }

                     

                ]]>

           </fx:Script>

           <local:Comp onMenuChange="comp1_onMenuChangeHandler(event)"/>

      </s:Application>

      any help will be welcome !

        • 1. Re: Custom component and custom event problem
          UbuntuPenguin Level 4

          Something seems fishy about your custom event class.  You don't declare whether it bubbles, is cancelable and so forth.  I would look at this link to see how they create a custom event class.  Notice how the super method is invoked ,

          http://curiousmindsmedia.wordpress.com/2008/05/29/actionscript-custom-event-tutorial/.

          The second thing I would do , is go into debug mode , and make sure my event handler for the navigator is being called.  You would be surprised how many times that happens.

           

          P.S. The formatting makes it hard to read the code

           

          Sincerely ,

            Ubu

           

          If this question was helpful , please mark it as succh

          • 2. Re: Custom component and custom event problem
            tweek214 Level 1

            hello again andthank you for your answer ,

            i've tried modifying my constructor and making the bubbles and cancelable parameters obligatory , and tried calling my dispatcher with bubbles = true , but still have no result , i tried debugging my program but my custom event handler is not called all that sounds strange for me ,since i'm a new in flex development

            • 3. Re: Custom component and custom event problem
              David_F57 Level 5

              hi,

               

              Not sure why your custom event is not working, I tend not to worry about 'custom' events unless I need to transfer a lot of information.

               

              Here is a way to 'simplify' event management.

               

              I just declare a new event in the meta data and then send it to notify that a change has been made. Although the parent can find the index value easily enough I also use two-way bind the navigators index so that it is directly available for the application to manipulate/read .

               

               

               

              http://gumbo.flashhub.net/events/  source included

               

               

              David.

              • 4. Re: Custom component and custom event problem
                tweek214 Level 1

                thank you for your answers !! thanks to you i found the "mistake" , in fact while dispatching the event i had to put the same name in the custom event metadata and the custom event object

                <fx:Metadata>

                          [Event(name="onMenuChange",type="events.Ev")]

                     </fx:Metadata>

                 

                and while dispatching :

                dispatchEvent(new CustomEvent(""onMenuChange","someData"));

                 

                voila !

                thanks a lot !