5 Replies Latest reply on May 6, 2011 10:39 AM by Hadzel

    Open mxml from a string

    Hadzel


      I have my menu in a xml:

      <MenuItem className="ProposalDetails" title="Select Proposal" openType="list" toolTip="Select Proposals and Proposal Versions" roles="60" url="selectProposalIcon" />

      I want to open the className of my MenuItem when I click it:

      I can acess the className with= data.className.... but like a string.

      How I can transform that in a compont?

      I tried:

      protected function itemRenderer_clickHandler(event:MouseEvent):void

      {

       

      if( parentApplication.hasOwnProperty("pMainPanel"))

      {

           ModelLocator.Instance.backView = parentApplication.pMainPanel;

           parentApplication.pMainPanel.removeAllElements();

       

       

       

       

             var dispObjName:String = data.className;

       

      var ClassReference:Class = getDefinitionByName(dispObjName) as Class;

      var classFactory:ClassFactory = new ClassFactory(ClassReference);

      var component:* = classFactory.newInstance();

       

      parentApplication.pMainPanel.addElement(component);

       

            }

      }

      But does't work.

        • 1. Re: Open mxml from a string
          Lee Burrows Level 4

          hi

           

          you haven't said how it doesn't work - are you getting error messages?

           

          i would create the component this way:

           

          var ClassReference:Class = getDefinitionByName(dispObjName) as Class;

          var component:IVisualElement = new ClassReference() as IVisualElement;

          • 2. Re: Open mxml from a string
            Hadzel Level 1

            Yeah, if I do this :

             

            var ClassReference:Class = getDefinitionByName("mx.controls.Button") as Class;
            var component:IVisualElement = new ClassReference() as IVisualElement;

             

            It works.

             

            But I set the parameter of getDefinitionByName as "modules.Project.ProposalApproval", witch is a titleWindow, and it returns this error = "Variable ProposalApproval is not defined."

            • 3. Re: Open mxml from a string
              Lee Burrows Level 4

              you may need to help out the compiler by adding references to the classes you are instantiating. such as:

               

              var comp1:ProposalApproval;

              var comp2:OtherMenuComp;

               

              (you can put these anywhere in your app)

               

              if there are no references to a class, it wont get included when compiling - hence runtime "not defined" error

              • 4. Re: Open mxml from a string
                Hadzel Level 1

                My ideia was to get xml's propertie with the name of the conatiner that I created and add it in my panel:

                 

                parentApplication.pMainPanel.addElement(data.className);

                 

                But flex doesn't find the references of my screen( ProposalAproval ) and reads 'data.className' as string and not a contatiner( TileWindow ). I didn't want to do a lot of 'if's' or 'switch-cases' to get the right container, because there are to many itens on my xml. hahahaha

                • 5. Re: Open mxml from a string
                  Lee Burrows Level 4

                  addElement takes an instance of a class - not a class itself - and the only way to generate an instance of a class from a string is to use the getDefinitionByName technique