4 Replies Latest reply on Nov 25, 2010 2:43 AM by Maximist

    how to get ItemRenderer Events

    Maximist Level 1

      Hallo, i have a little problem. In my application i am using a List and build his elements with an itemRenderer. In this itemRenderer i have on each line, 1 label and 1 button. When i push on the button i want to dispatch that event in my main application.. How can i create a communication between myApplication and itemRenderer ??

       

      Thx for all

       

      Max

        • 1. Re: how to get ItemRenderer Events
          BhaskerChari Level 4

          Hi Max,

           

          Dispatch the event by setting the bubbles property of the event object to true so that you can listen for that event in the main application.

           

          In your itemRenderer dispatch an event as shown..

           

          <!-- Itemrenderer -->

          this.dispatchEvent(new Event("rendererBtnClicked", true));

           

          <mx:Application creationComplete="init()">

           

          private function init():void
            {
             this.addEventListener("rendererBtnClicked", btnClickHandler);
            }

           

          private function btnClickHandler(event:MouseEvent):void
            {
                    //Write your logic here

            }

           

          </mx:Application> 

           

          If you want to pass data along with the event then create a Custom Event Class so that you can pass data along with the event Object.

           

           

          Thanks,

          Bhasker

           

           

           

           

          Message was edited by: BhaskerChari

          • 2. Re: how to get ItemRenderer Events
            flex2008 Level 3
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"
                            creationComplete="application1_creationCompleteHandler(event)">
                <mx:Script>
                    <![CDATA[
                        import mx.events.FlexEvent;
                        
                        private var array:Array = ['1','2','3','4','5','6','7','8','9','10'];
                        
                        protected function application1_creationCompleteHandler(event:FlexEvent):void
                        {
                            // TODO Auto-generated method stub
                            list.dataProvider = array;
                            this.addEventListener('eventFromIR',handlerForEventFromIR,true);
                        }
                        
                        private function handlerForEventFromIR(event:Event):void
                        {
                            trace('button click caught in application');
                        }
            
                    ]]>
                </mx:Script>
                <mx:List id="list" horizontalCenter="0" width="150">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:Button click="button1_clickHandler(event)">
            
                                <mx:Script>
                                    <![CDATA[
                                        
                                        override public function set data(value:Object):void
                                        {
                                            super.data = value;
                                            if(!value)
                                            {
                                                return;
                                            }
                                            label = 'Button'+value;
                                        }
                                        protected function button1_clickHandler(event:MouseEvent):void
                                        {
                                            trace('button click caught in item renderer');
                                            //If you want to pass some data to the application,create a custom event class with properties to hold your data.
                                            //then create an instance of the event with the required data here and dispatch that event.
                                            dispatchEvent(new Event('eventFromIR',true));
                                        }
                                    ]]>
                                </mx:Script>
            
                                
                            </mx:Button>
                        </mx:Component>
                    </mx:itemRenderer>
                </mx:List> 
            </mx:Application>
            
            

             

            you can use the above code.Let me know if you have any questions.

            • 3. Re: how to get ItemRenderer Events
              Maximist Level 1

              i receive this error:

               

              TypeError: Error #1034: Assegnazione di tipo forzata non riuscita: impossibile convertire flash.events::Event@33a788d1 in flash.events.MouseEvent.

              at flash.events::EventDispatcher/dispatchEventFunction()

              at flash.events::EventDispatcher/dispatchEvent()

              at mx.core::UIComponent/dispatchEvent()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\co re\UIComponent.as:12266]

              at componenti.Navigazione_Vendor_Eventi::mioItemRenCampiMultipliXListaEventi/compraBiglietti EventoHandler()[/Users/massimopalomba/Documents/Progetti/1_WOW/1_Admin/WOW/src/componenti/ Navigazione_Vendor_Eventi/mioItemRenCampiMultipliXListaEventi.mxml:65]

              at componenti.Navigazione_Vendor_Eventi::mioItemRenCampiMultipliXListaEventi/__compraBigliet tiEvento_click()[/Users/massimopalomba/Documents/Progetti/1_WOW/1_Admin/WOW/src/componenti /Navigazione_Vendor_Eventi/mioItemRenCampiMultipliXListaEventi.mxml:95]

              sorry if it is italian.. anyway it says that is not possible to convert  flash.events::Event@33a788d1 in flash.events.MouseEvent.
              The only thing i can say is that i am using spark components, you are using mx... Tha can be the problem?.. if yes, can i find a solution in spark?
              Thx
              Max

              • 4. Re: how to get ItemRenderer Events
                Maximist Level 1

                Ok sorry, it was my mistake. In the main application i wrote:

                 

                private function aPulsanteItemRendererClikkato(event:MouseEvent):void

                {

                     // logic

                }

                 

                it is not evet:MouseEvent but event:event.

                 

                Thx for the help

                 

                Max