5 Replies Latest reply on Feb 26, 2010 2:02 AM by Karl_Sigiscar_1971

    Refer to Canvas ID with variable

    jhough76

      I have a

      7 x 6 Grid and each griditem consists

      of a canvas object. I have named the canvas id for grid

      item 1, 1 to be canvas1_1, griditem 1, 2 to be canvas1_2, etc.

       

      I would like to iterate through the canvas objects using two for loops and set attributes, i.e.

       

      for ( i=1; i<7; i++ )

          for ( j=1; j<8; j++ )

          {

              canvasobject = "canvas" + i + "_" + j;

       

              canvasobject.backgroundColor = "#ffffff";

          }

       

      The syntax above is not working as canvasobject is of type string. Is there anyway to Cast the String object to a type of canvas or enumerate the canvas objects in order to obtain the object in a variable?

       

      Any help is appreciated.

       

      -Jack-

        • 1. Re: Refer to Canvas ID with variable
          Karl_Sigiscar_1971 Level 3

          Question:

          Why do you re-invent the TileList component and item renderer concept ?

           

          Also, you need to keep the number of containers (and nesting) in your application to a minimum.

           

          Do you really need a Canvas for each item ?

          • 2. Re: Refer to Canvas ID with variable
            jhough76 Level 1

            Great question. I worked with the TileList control and could not get the look the way I wanted it. Using the Grid Item and canvas solution seems to be perfect as far as the look goes. I did not consider the overhead of having 42 canvas objects when making this decision.

             

            Regardless, wouldn't I run into the same issues trying to refer to an object as a variable and trying to update it's properties?

             

            -Jack-

            • 3. Re: Refer to Canvas ID with variable
              Karl_Sigiscar_1971 Level 3

              Each item renderer instance in your TileList would be bound to an object in an Array Collection (the data provider) via the data property.

               

              What is great with the item renderer concept is that each item can manage itself. An item renderer can and should be oblivious of the world around it. Therefore, you do not neet any outside class to control it and therefore your do not need any reference to it.

               

              If however, you still need to have communication between your item renderers and another part of your application, you can still bind each item renderer to a presentation model or simply use events dispatched on a globally accessible target object (e.g in a model) that each item renderer can listen to. When the user clicks on an item renderer, you can dispatch an event that bubbles up and is caught higher in the display list (e.g in the TileList itself or higher).

               

              You might also look at an inversion of control (IoC) framework and use dependency injection.

              • 4. Re: Refer to Canvas ID with variable
                jhough76 Level 1

                Thank you for the response. I will look into your suggestion as it seems like it is the right way to solve my issue. At this point it is not a quick change, but if I have to, I will. Are you suggesting this direction because of "best practices" or out of necessity. In other words, is what I am trying to accomplish with the Grid and canvas objects impossible?

                 

                Thanks again

                Jack

                • 5. Re: Refer to Canvas ID with variable
                  Karl_Sigiscar_1971 Level 3

                  Hi Jack,

                   

                  I'd say the advice is both out of necessity and out of best practices.

                   

                  You said:

                  I would like to iterate through the canvas objects using two for loops and set attributes,

                   

                  So, in the case of the TileList / ArrayCollection solution, you would iterate through the objects of the data provider (ArrayCollection) of the TileList and change their properties when required. This would automatically reflect in the item renderers. So, there is no need to reference them.

                   

                  In order to get the look you need, explore all the skinning and styling options available for item renderers.