7 Replies Latest reply on Jul 7, 2006 5:43 PM by inlineblue

    Dynamic Components By Name

    BlueRealm
      I'm trying to find out if you can create dynamic objects on the fly by name. I want to populate the children of a view stack with data from an xml file. I've not been able to create dynamic objects by name. Is there a way to create a new instance of an object by its name?

      Such as:

      var test:DisplayObject = new getObjectByName("HomeView");
      this.storeViews.addChild(test);

      <mx:ViewStack id="storeViews" width="100%" height="550" />

      This works just fine if i hardcode it:

      var test:DisplayObject = new HomeView;
      this.storeViews.addChild(test);

      <mx:ViewStack id="storeViews" width="100%" height="550" />

      I just want to know if flex or as3 are dynamic enough to create data driven variables,objects,classes?
        • 2. Dynamic Components By Name
          BlueRealm Level 1
          Well getClassByName doesnt exist in AS3 flash.util. anymore. Its changed to getDefinitionByName. But it doesnt work with any flex classes. That one only works with flash classes.

          So if i do var test:Class= new getDefinitionByName("String"); it works
          but if i do
          So if i do var test:Class = new getDefinitionByName("Canvas"); it says Canvas is undefined.
          • 3. Re: Dynamic Components By Name
            inlineblue Level 1
            A couple of things you have to make sure you do:

            - Use the fully qualified class name, ie. mx.containers.Canvas

            - The class you are trying to get must be available at runtime, which means it must be linked into the SWF. To ensure this, you must at least declare something of that class in your app. There's no way around this.
            • 4. Dynamic Components By Name
              BlueRealm Level 1
              Yes I've tried that and it works... But my problem is it doesnt work for custom components. Is there away to get that to work? The reason I want to be able to do this is so I can load the Custom component name (string) from the db and load the corsponding pages.

              This works

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" minHeight="650" minWidth="880" creationComplete="init()">
              <mx:Script>
              <![CDATA[
              import flash.utils.getDefinitionByName
              import app.HomeView
              import mx.containers.Canvas
              private function init() : void
              {
              var ClassReference:Class = getDefinitionByName(" mx.containers.Canvas") as Class;
              var newCanvas:DisplayObject = new ClassReference;
              this.myHBox.addChild(newCanvas);
              }
              ]]>
              </mx:Script>

              <mx:HDividedBox width="100%" height="100%" id="myHBox">
              </mx:HDividedBox>

              </mx:Canvas>

              This give an undefined error on HomeView


              <?xml version="1.0" encoding="utf-8"?>
              <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" minHeight="650" minWidth="880" creationComplete="init()">
              <mx:Script>
              <![CDATA[
              import flash.utils.getDefinitionByName
              import app.HomeView
              import mx.containers.Canvas
              private function init() : void
              {
              var ClassReference:Class = getDefinitionByName(" app.HomeView") as Class;
              var newCanvas:DisplayObject = new ClassReference;
              this.myHBox.addChild(newCanvas);
              }
              ]]>
              </mx:Script>

              <mx:HDividedBox width="100%" height="100%" id="myHBox">
              </mx:HDividedBox>

              </mx:Canvas>
              • 5. Dynamic Components By Name
                inlineblue Level 1
                You have to at least declare an object of a class in order to have that class available at runtime (linked in). It's not enough to just import it. So somewhere in your app do this:

                var x: app.HomeView;
                • 6. Re: Dynamic Components By Name
                  BlueRealm Level 1
                  Awesome... That works perfect... But it still is'nt as dynamic as i would like. Is there a way to call an exnteral as file that would then set my variables at runtime?

                  So if i had coldfusion generate an as file that looped through my pages table and created an as file like such:

                  // ActionScript file
                  package app{
                  public class LoadComponents{
                  public var x:ContactList;
                  public var y:Journals;
                  }

                  }
                  • 7. Re: Dynamic Components By Name
                    inlineblue Level 1
                    Uh no. Now you're talking about dynamic compilation of source code and that's not going to happen. You'll have to settle with loading up a text file that specifies the components to create (possibly a list of class names).