5 Replies Latest reply on Apr 10, 2009 7:49 AM by Handycam

    Naming components created in a loop?

    Handycam Level 1

      I asked this once before, but none of the answers work in this case.

       

      I am creating a series of components as children of a viewstack based on XML loaded.  I am looping through the XML and instancing a component for each node.

       

      private function createSteps():void{

      var stepNumber:uint = 1;

      var numSteps:uint = dataXML.length;

       

              for each(var step:XML in dataXML){

      var recipeStep:CyorStep = new CyorStep();

      mainViewStack.addChild(recipeStep);

      recipeStep.label = stepNumber +". "+ step.stepTitle;

       

      recipeStep.itemChoicesList = step..item;

      recipeStep.prompt.text = step.prompt;

      recipeStep.stepCaption.text = step.bodyText;

      recipeStep.stepTitle.text = step.stepTitle;

       

      if (stepNumber == numSteps){

      recipeStep.nextBtn.label = "FINISH";

      }

      recipeStep.addEventListener("GO_PREVIOUS",prevStep);

      recipeStep.addEventListener("GO_NEXT",nextStep);

      stepNumber++

      }

            }

       

       

       

      The problem is all these objects have randomly generate names.  How can I assign each object a name as I instance it, as in:

       

      var recipeStep1:CyorStep = new CyorStep();

      var recipeStep2:CyorStep = new CyorStep();

       

      and so on?

       


       

       

        • 1. Re: Naming components created in a loop?
          Gregory Lafrance Level 6

          Define the constructor to take a parameter, and set this.name and/or super.name. But I don't think you can do this in MXML, so implement the compoenent as an AS class.

          • 2. Re: Naming components created in a loop?
            Handycam Level 1

            Wouldn't I still have the same naming and scoping problem?

             

             

            // Create and add steps

            private function createSteps():void{

            var stepNumber:uint = 1;

            var numSteps:uint = dataXML.length;

             

             

            for each(var step:XML in dataXML){

            var recipeStep:CyorStep = new CyorStep("step"+stepNumber); 

            ...

             

            If I did that could I

             

            1. Give it a sequential name somehow
            2. Be able to access it OUTSIDE of this function
            • 3. Re: Naming components created in a loop?
              Gregory Lafrance Level 6

              I think in that case you would need to put the instances in some storage mechanism outside the function, like a collection or object. If the names are unique, you could use an associative array object:

               

              myStorageObj[myStepObj1Name] = mysStepObj1;

              myStorageObj[myStepObj2Name] = mysStepObj2;

              ...

              • 4. Re: Naming components created in a loop?
                Handycam Level 1

                OK, close.  What I have done is created an array to hold them:

                 

                [Bindable]

                public var stepModules:Array = [];

                 

                 

                Then modified my creation loop like this:

                 

                private function createSteps():void{

                var stepNumber:uint = 1;

                var numSteps:uint = dataXML.length;

                var recipeStep:CyorStep;

                        for each(var step:XML in dataXML){

                //var recipeStep:CyorStep = new CyorStep();

                recipeStep = new CyorStep();

                recipeStep.name = "recipeStep"+stepNumber.toString();

                mainViewStack.addChild(recipeStep);

                recipeStep.label = stepNumber +". "+ step.stepTitle;

                recipeStep.itemChoicesList = step..item;

                recipeStep.prompt.text = step.prompt;

                recipeStep.stepCaption.text = step.bodyText;

                recipeStep.stepTitle.text = step.stepTitle;

                ...etc...

                stepModules.push(recipeStep);

                stepNumber++;

                        }

                        trace(stepModules[0].stepCaption.text);

                      }

                 

                This trace statement gives me the caption of the first item created, fine.  However, I have a datagrid that needs to be updated with the contents of the "drop" datagrid of the drag-n-drop in the component, so I tried setting its dataprovider to dataProvider="{stepModules[0].chosenList.dataProvider}" but I get nothing.  I can't figure out why, or if there's a step I need to do to get the dropped items to appear in the second grid as well as where they're dropped.

                • 5. Re: Naming components created in a loop?
                  Handycam Level 1

                  As an addendum, if I replace that trace statement with:

                   

                   

                  trace(stepModules[2].chooseList.dataProvider);

                   

                   

                  I get

                   

                  <item sn="potatoes" ln1="Potatoes" ln2="new potatoes, cut into medium dice" qty="1" units="lb."/>

                  <item sn="egg noodles" ln1="Egg noodles" ln2="egg noodles" qty="2" units="cups"/>

                  <item sn="white beans" ln1="White beans" ln2="15-1/2 or 16-oz. cans white beans, drained and rinsed" qty="2" units=""/>

                  <item sn="chickpeas" ln1="Chickpeas" ln2="15-1/2 or 16-oz. cans chickpeas, drained and rinsed" qty="2" units=""/>

                  <item sn="hominy" ln1="Hominy" ln2="15-1/2 or 16-oz. cans hominy, drained and rinsed" qty="2" units=""/>

                  <item sn="rice" ln1="Long-grain white rice" ln2="long-grain white rice" qty="1/3" units="cup"/>

                  <item sn="rice" ln1="Basmati rice" ln2="basmati rice" qty="1/3" units="cup"/>

                  <item sn="orzo" ln1="Orzo" ln2="orzo pasta" qty="1/3" units="cup"/>

                   

                  which is correctly the contents of the left (drag from) list.

                   

                  However, trace(stepModules[2].chooseList.dataProvider.@sn) throws an error Error: Unknown Property: 'sn'.