7 Replies Latest reply on Mar 18, 2007 7:18 AM by darkfrog26

    Accessing objects by ID

    Kwooda Level 1
      I have assembled an ID for an object based on some data provided through an event handler, and I want to use that ID to access the object. How do I do that? If you KNOW the ID at authoring time, you can just say myObject.property=something (if myObject is the ID), but I need to do stuff dynamically. In JavaScript in the web world, I can use getElementById(id) - what is the equivalent in Flex?
        • 1. Re: Accessing objects by ID
          ntsiii Level 3
          Use "bracket notation":
          var sObjectId:String = "obj" + SomeVariableOrExpression;
          var oMyObject:Whatever = this[sObjectId];

          This works wit almost every object in flex. Just change "this", which refers to the current parent object, with whatever object contains the element you want.

          • 2. Re: Accessing objects by ID
            Kwooda Level 1
            Tracy -

            OK, I think my real question was not conveyed well. I have some components in my application that I have given an id, and I have an event handler that responds to an event. In that event handler, it builds an ID string, dynamically, that matches the ID of one of my components. Now...how do I use that ID to address the desired component? In other words, what is the object I use in place of "this" in your example?

            In JavaScript, I know I have a DOM to work with, and that page elements are attached to a root document object, which I can reference in JavaScript using document.getElementById(id). I am looking for the equivalent in Flex to access components that are part of my application UI. I am at a loss as to what that base "document" object is and how to reference components from there. In Delphi, there was a controls array and a components array - is there anything comparable in Flex?
            • 3. Re: Accessing objects by ID
              ntsiii Level 3
              Flex also has a DOM.

              The base is the top-level Application.

              "this" refers to the component scope in which it is used.

              If the component you want to reference in declared in the main application file, like this:
              <mx:Application ...
              <myComponent id="mycomp99" ...

              Then from a script block in the main app, you can do:
              var iNumber:int = 99;
              var sObjectId:String = "mycomp" + iNumber;
              var oMyObject:myComponent = this[sObjectId]; //"this" refers to the main app scope

              oMyObject will contain a live reference to the component identified by id="mycomp99"

              If mycomp99 is in a sub component of the main app, and that sub-component has an id, say "sub1", then refer to "mycomp99" like this:
              var iNumber:int = 99;
              var sObjectId:String = "mycomp" + iNumber;
              this.sub1[sObjectId]; //"this" is optional/understood in this context


              • 4. Accessing objects by ID
                Kwooda Level 1
                So it seems, then, you must have some advance knowledge of where the target component resides so you can construct the correct reference (this.x, this.sub.x, etc.). I take it, then, there is no equivalent to getElementById(id) or some global components array or such? I mean, it seems the whole point of having an ID is so you can reference the component in some direct way without having to know exactly where it lives.
                • 5. Re: Accessing objects by ID
                  ManMachine Level 1
                  Two answers:

                  1. You could build a recursive function (you could allways name it getElementById) that iterates through each child of the application and each child of each child etc until it finds and returns the element with the id you're looking for. All children and the app are UIComponents so typing is no problem.
                  2. Use Cairngorm and report all relevant views to the ViewLocator.

                  I would recommend solution no 2. It's far better practice.

                  • 6. Re: Accessing objects by ID
                    this[myId] works to get a component anywhere in the application. I'm doing more advanced things in my code so I actually create an Object that contains references to all of my components as well and I use that to get them back out because I may want to remove something from the scene and then add it back in.
                    • 7. Re: Accessing objects by ID
                      darkfrog26 Level 1
                      ...to clarify:
                      this[myId] == document.getElementById(myId)