3 Replies Latest reply on Oct 7, 2009 5:56 PM by Flex harUI

    Use XML as a component reference

    Rakmaster 606

      I'm trying to load a series of custom components into an mxml using an XML as my reference. In PHP I would simply eval() the object name and that would be that. Flex doesn't have an eval. I'm wondering if there's a way for me to do something like this:

       

      function readXML(theXML) {

           for each(var x in theXML.children()) {

                var newComp = eval(x.@name);

                myViewStack.addChild(newComp);

           }

      }

       

      Anybody?

        • 1. Re: Use XML as a component reference
          Flex harUI Adobe Employee

          ApplicationDomain.currentDomain.getDefinition(x.@name)

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Use XML as a component reference
            Rakmaster 606 Level 1

            Well, almost...

             

            TypeError: Error #1034: Type Coercion failed: cannot convert [myCustomContainerName]$ to mx.containers.Canvas.

             

            The custom containers are created using the Canvas class. I even tried using "as Canvas" after calling the getDefinition. Nothing seems to work. I've been at this for HOURS now and I'm feeling really stupid. I'm going to attach my source this time.

             

            Thx.

             

            I'm not sure the attachment worked so:

             

            public function drawComponents(x:XML):void
                        {
                            mainViewStack = new ViewStack();
                            for each(var y:XML in x.item) {
                                if(ApplicationDomain.currentDomain.hasDefinition(y.@name) == true) {
                                    var newChild:Canvas = new Canvas();

                                    var newLabel:String = y.@label;
                                    newChild = Canvas(ApplicationDomain.currentDomain.getDefinition(y.@name));
                                    newChild.label = newLabel;
                                } else {
                                    Alert.show("not defined");
                                }
                                mainViewStack.addChild(newChild);
                            }

            }

            • 3. Re: Use XML as a component reference
              Flex harUI Adobe Employee

              That sounds like an applicationDomain problem.  Are you dynamically loading these classes?  See the modules presentation on my blog.  Maybe the Marshall Plan presentation as well.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui