2 Replies Latest reply on Dec 30, 2009 9:07 AM by RK...

    Calling Flex components @ runtime

    nitinkcv Level 1

      Hi all,

       

      Am new to flex so kindly bear with my questions even though they might seem trivial to you!!

       

      I'll briefly explain my use case to you.

       

      I'm making use of Flex3.  I have an xml file which i'm parsing out. The rough structure of my xml file is:

       

      <element id='one'/>

      <element id='two' relation='list'>

      <element id='three' relation='list'>

      <element id='four'>

      <element id='five relation='map'>... and so on.

       

      While parsing all these values of the id attribute would be presented in a mx:list.

       

      So on click of each of the id's on the mx:list and depending on the type of the 'relation' attribute i need to display certain components.

       

      So say when no 'relation' is present i would not show anything.

      For the relation=list i would have a show a mx:list along with three buttons.

      For say relation=map iwould have to show simply a mx:list.

      and the values for these relation may be added later on.


      So i would have to insert these UI:components on click of each item in the link.

       

      Could anyone let me in on how to achieve this?

       

      Thanks.

        • 1. Re: Calling Flex components @ runtime
          RK... Level 3

          Can you try this,

           

          <mx:HBox width="100%" height="200">
               <mx:List id="lst" width="50%" height="100%" dataProvider="{lstDP.element.@id}" change="changeHandler(event)">
               </mx:List>
               <mx:VBox id="vb" width="50%" height="100%" visible="false">
               </mx:VBox>
          </mx:HBox>
          

           

          private function changeHandler(event: Event): void {
               var str: String = lstDP.element[lst.selectedIndex].@relation;
               vb.removeAllChildren();
               vb.visible = false;
               if (str) {
                    vb.visible = true;
                    switch(str) {
                         case "list":
                              var btn: Button = new Button();
                              btn.label = "Button";
                              vb.addChild(btn);
                              //Add your components
                         break;
                         case "map":
                              var lbl: Label = new Label();
                              lbl.text = "Map";
                              vb.addChild(lbl);
                              //Add your components
                         break;
                    }
               }
          }
          private var lstDP: XML = <root>
                                             <element id='one'/>
                                             <element id='two' relation='list'/>
                                             <element id='three' relation='list'/>
                                             <element id='four'/>
                                             <element id='five' relation='map'/>
                                        </root>
          

          • 2. Re: Calling Flex components @ runtime
            nitinkcv Level 1

            Thanks a lot.

             

            I'll try this out and let you know.

             

            I had browsed through some articles and did see another post @ http://flexinonroids.wordpress.com/2009/05/27/flex-3-dynamically-loading-components-at-run time/ in which they make use of Modules to load components # run time. I tried to understand whatever they were mentioning.

             

            If possible could you please let me know how these two are different and  whether we can say which of the two would be better?

             

            Thanks.