7 Replies Latest reply on Dec 8, 2009 12:43 AM by paul.williams

    Dispatching an Event manually

    Shivik Level 1

      Hello all

       

      I am stuck at this simple problem being a newbie in ActionScript coding. Here is a snippet of my problem...

       

      <mx:Script>
           <![CDATA[
                public function func1( event:ItemClickEvent ):void { // some code
                }
                public function func2( event:FlexEvent ):void {
                     // Somehow generate itemClick of tabbar here so that func1 is called
                }
           ]]>
      </mx:Script>
      
      <mx:TabBar id="tabbar" itemClick="func1(event)" updateComplete="func2(event) />
      
      

       

      How can I generate the itemClickEvent of this TabBar using actionscript?

       

      Thanks and Regards

      ShiVik

       

      Rephrased my question

        • 1. Re: Dispatching an Event manually
          Shivik Level 1

          After a bit of poking around I found out this method of doing what I want....

           

          <mx:Script>
               <![CDATA[
                    public function func1( event:ItemClickEvent ):void { // some code
                    }
                    public function func2( event:FlexEvent ):void {
                         // Somehow generate itemClick of tabbar here so that func1 is called
                         var tabbar:TabBar = TabBar( event.currentTarget );
                         tabbar.dispatchEvent( new ItemClickEvent( ItemClickEvent.ITEM_CLICK, false, false ) );
                    }
               ]]>
          </mx:Script>

          <mx:TabBar id="tabbar" itemClick="func1(event)" updateComplete="func2(event) />

           

          But on executing the code, it gives the following error....

          Error: Unknown property: '-1'
           at mx.collections.ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()
          

           

          Can somebody help me out with this?

           

          Thanks and Regards

          ShiVik

          • 2. Re: Dispatching an Event manually
            vertex_shader Level 1

            Looks like ur missing double quotes in:

            updateComplete="func2(event) />

             

            You'll also need imports for those events. For example, you can put your cursor at the end of ItemClickEvent and hit Ctrl+Space to automatically add the corresponding import.

            1 person found this helpful
            • 3. Re: Dispatching an Event manually
              flex2008 Level 3

              When an itemClick is dispatched it will contain the information regarding which item has been clicked.index property in ItemClickEvent will give this information.This is when the control dispatches the itemClick event.Here you are manually trying to dispatch itemClick.You havent specified index value in the constructor.So default value of -1 is taken and that is why you are getting an error.Just give some value to index say 0(this means you are simulating the situation where you have clicked the first tab.For 1st tab,index = 0)

              • 4. Re: Dispatching an Event manually
                Shivik Level 1

                Hello there

                 

                That must have been a typo here. It's there in my code. Also, I have imported the ItemClickEvent and FlexEvent already - Flex Builder did it for me.

                 

                The error is still there though

                 

                Thanks and Regards

                ShiVik

                • 5. Re: Dispatching an Event manually
                  Shivik Level 1

                  Hello

                   

                  I think the deal-breaker here is the dataProvider of TabBar. I am getting the data from a xml file. Could that be the reason?

                   

                  <mx:Script>
                       <![CDATA[
                            // import classes here
                            public function func1( event:ItemClickEvent ):void {
                                 var tabbar:TabBar = TabBar( event.currentTarget );
                                 mx.controls.Alert.show( 'selectedIndex = ' + tabbar.selectedIndex.toString() );
                  
                            }
                            public function func2( event:FlexEvent ):void {
                                 var tabbar:TabBar = TabBar( event.curretnTarget );
                                 tabbar.selectedIndex = 0;
                                 tabbar.dispatchEvent( new ItemClickEvent( ItemClickEvent.ITEM_CLICK, false, false, null, 0 ) );
                            }
                       ]]>
                  </mx:Script>
                  
                  <mx:Tabbar dataProvider={service.getData()} labelField="label"
                       itemClick="func1(event)" updateComplete="func2(event)" />
                  

                   

                  When I load the swf, it shows two Alert boxes.

                  One shows selectedIndex = 0

                  the other shows selectedIndex = -1

                   

                  The data is loading just fine. So I know that the service is working.

                   

                  Please help me out with this...

                   

                  ShiVik

                  • 6. Re: Dispatching an Event manually
                    paul.williams Level 4

                    I think flex2008 has already given you the answer. You are dispatching an ItemClickEvent with an index of -1. This doesn't make sense - you cannot click on a negative item. The constructor for the event is:

                     

                    ItemClickEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, label:String = null, index:int = -1, relatedObject:InteractiveObject = null, item:Object = null)

                     

                    You need to set the fifth property "index" correctly, otherwise it is initialized to -1.


                    Why are you simulating an ItemClick in this way?

                    1 person found this helpful
                    • 7. Re: Dispatching an Event manually
                      Shivik Level 1

                      @paul.williams

                       

                      I have a scenario where I need to load some data according to the tab that has been selected. That task is usually done on ItemClickEvent. So I want the same to happen when I set the default tab.

                       

                      Anyway, I think my problem has been solved now. I had a creationComplete event in the Application tag that was the reason behind my troubles. I used the initialize event instead and it worked.

                       

                      Thanks and Regards

                      ShiVik