1 Reply Latest reply on Jul 26, 2007 5:56 PM by peterent

    Instantiating a Custom Component

    emansouri
      I am somewhat new to this concept and I keep thinking about things the ActionScript 2 way where we can say:

      createObject(refObject,instanceName,depth,constructorObject);

      I am instantiating custom MXML components using a technique similar to the following:

      var classRef:Class = flash.utils.getDefinitionByName("Components."+module) as Class;
      var d:DisplayObject = new classRef() as DisplayObject;
      mainCanvas.addChild(d);

      where mainCanvas is a Canvas component on my main stage and d is a reference to the custom component attached to the mainCanvas.

      My question is what is the best practice means to pass data to d such that it will be available upon instantiation. I am looking to employ the constructor object concept from attachMovie and createObject of AS 2.

      Thanks in advance
        • 1. Re: Instantiating a Custom Component
          peterent Level 2
          Look up ClassFactory - but what you are doing isn't very Flex-ish. The better way is to do:

          var d:Whatever = new Whatever();
          d.property1 = value;
          d.property2 = value;
          ...
          mainCanvas.addChild(d);

          This is the way we do it within the Flex framework and is pretty easy to understand. You might also want to look up the commitProperties function and override that in your custom component in case you need to combine or use your property values while your component is initializing.