7 Replies Latest reply on Oct 19, 2010 12:51 PM by real_ideveloper

    How can I use the CustomEvent in custom Item Renderer

    real_ideveloper

      Hi all.

       

       

      I have a small problem, which i must solve.

       

       

      I have a ImageRenderer - TileList with a custom ItemRenderer.

      When I choose a picture a button appears (in the ItemRenerer) and when I click it it throws an event with the name of the picture.

       

      The Problem is, that i can not add an event Listener to the ItemRenderer.

      Can anybody help me to resolve this problem?

        • 1. Re: How can I use the CustomEvent in custom Item Renderer
          Claudiu Ursica Level 4

          The Problem is, that i can not add an event Listener to the ItemRenderer.

          Why can't you add listener?

           

          C

          • 2. Re: How can I use the CustomEvent in custom Item Renderer
            real_ideveloper Level 1

            Bacause the itemRenderer must be an element of IFactory

            ...

            itemRenderer = "new ClassFactory(ThumbnailRenderer)"

            ...

             

            And there are no methonds but only the newInstance()

             

            ...

            this.itemRenderer.newInstance()

            ...

            • 3. Re: How can I use the CustomEvent in custom Item Renderer
              Claudiu Ursica Level 4

              I might be missing bits and pieces however wherever you set this line

               

              itemRenderer = "new ClassFactory(ThumbnailRenderer)"

               

              you should be inseide a list or inside some UIComponent (Gruop or whatever)

               

              so if you dispatch a custom event from inside the ThumbnailRenderer you can let

              it bubble to the list or that component....

              Again maybe I am missing something...

               

              C

              • 4. Re: How can I use the CustomEvent in custom Item Renderer
                saisri2k2 Level 4

                Could you please paste the renderer code here?

                • 5. Re: How can I use the CustomEvent in custom Item Renderer
                  real_ideveloper Level 1

                  But how can I catch it in the top of the list??????

                   

                  The ItemRenderer is a IFactory Class Object, which has only one function newInstance().

                   

                  I repeat it again, there is no addEventListener or something else for the ItemRenderer.


                  I can give you only a header of the code because it is a big project (and if somebody have a solution or an example, maybe he could give me the it, please)

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:TileList xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();"
                       dataProvider="{imageList}"
                       labelField="label"
                       iconField="thumbnail"
                       itemRenderer="{new ClassFactory(ThumbnailRenderer)}" verticalScrollPolicy="auto">

                  ......

                   

                   

                  and the renderer

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
                            horizontalAlign="center" verticalAlign="middle" creationComplete="init()"
                            xmlns:DataModels="DataModels.*"
                            click="selectedHandler()" height="125">

                       <mx:Image source="{data.thumbnail}"
                                width="80"
                                height="50"

                                      horizontalCenter="0"
                                   verticalCenter="0" horizontalAlign="center" verticalAlign="middle"/>

                       <mx:Label text="{data.label}"
                                 horizontalCenter="0"
                                 bottom="0" />
                       <mx:Metadata>
                           [Event(name="addImageEvent", type="DataModels.AddImageEvent")]
                      </mx:Metadata>

                   

                  The button is created dynamicly. When i clicked it, then i dispatch a new AddImageEvent

                   

                  like this

                   

                  ...

                  button.addEventListener(MouseEvent.CLICK, dispatchAddImageEvent);

                  ...

                  private function dispatchAddImageEvent(event:Event):void

                  {

                       var addImageEvent:AddImageEvent = new AddImageEvent(AddImageEvent.ADD_IMAGE_EVENT);

                       dispatchEvent(addImageEvent);

                  }

                   

                  Inside the renderer i can catch it, because there is the addEventListener method, but not in the ITEMRENDERER (IFactory element)

                  • 6. Re: How can I use the CustomEvent in custom Item Renderer
                    Claudiu Ursica Level 4

                    NOw I am pasting code modified from some project however you'll get the idea.

                     

                     

                    inside item renderer:

                     

                        private function clickHandler(event:MouseEvent):void

                             {

                                    event.stopImmediatePropagation();

                                    dispatchEvent(new CustomEvent(CustomEvent.ADD_IMAGE,

                    PayloadObjectIfNecessary, true));

                             }

                     

                     

                    Notice the true for bubbling.

                     

                    inside the tile list you can register for the event (onCreationOmplete or othere

                    method)

                     

                    this.addEventListener(CustomEvent.ADD_IMAGE, handleEventInsideTileList);

                     

                    inside tileList's parent

                     

                    this.tileList.addEventListener(CustomEvent.ADD_IMAGE,

                    handleEventInsideTileListsParentOnTheListInstance);

                    assuming tileList is the TileList's id

                     

                    or listen ont the tileList parent

                     

                    this.addEventListener(CustomEvent.ADD_IMAGE,

                    handleEventInsideTileListParentOnTheParentInstance);

                     

                     

                     

                    C

                    • 7. Re: How can I use the CustomEvent in custom Item Renderer
                      real_ideveloper Level 1

                      Thank you.

                      Setting the Bubble to true was the solution.