views:

2405

answers:

7

I want to dynamically access a bunch of objects in my mxml. I can construct their name/id. In Javascript I can do "getElementById(ID)" to get the object. How can I do this in Actionscript?

I really can't do getChildByName because it is too cumbersome: I have access to object A, which has a child B, which has a child C, which have the children D, E & F (their names are related to A's name). I want to get D, E & F. For getChildByName, it seems I have to use A to get B, then get C, and then get D, E & F. And if add a new parent to B (change the mxml hierarchy), then the code will break... And I really don't want to do that.

Any advice? Thx!

Update: What I am asking is, how do I access object D given its name and/or id, both of which are strings.

A: 

If you use MXML you must be using flex. If you are using Flex then you can assign your items id's.

Example:

<mx:Button
      id    = "myButton"
      label = "OK"
      click = "{myLabel.text = 'Button Pressed!'}"/>

<mx:Label
      id    = "myLabel"
      text  = "Push the button!"/>
Adrian Pirvulescu
Note: the above is equivalent to saying: var myButton:Button = new Button(); When you give an mxml component an id, you are really making an actionscript variable
Sean Clark Hess
+5  A: 

Since the children are actually like "properties" of the document, you should be able to do something like this:

var elem:Type_of_E = this["constructed_id_of_E"];

If you are in a subdocument of the document just use parentDocument["constructed_id_of_E"] to get the element.

Cristian Ivascu
Thanks! Exactly what I was looking for!
sri
+1  A: 

Actually, once you assign them an ID, they become a public member variable of the given element. So for instance if you have

<mx:Canvas><mx:Label id="myLabel" /></mx:Canvas>

You can access it by it's id as a variable name:

myLabel.text = "Hello world!";

Let me know if you need more clarification. If this isn't what your asking, feel free to comment and I'll take another whack at it.

Regards, Chris

Update: All mxml components are accessible this way, regardless of how many parents they have

Chris Thompson
+1  A: 

Hello,

Does this work with Flex 4 ?

I've an empty grid made in MXML, then in actionscript I add in this order :

  • a gridrow (inside the grid) (id="myGrid")
  • a gridItem (inside the gridRow)
  • a combobox (inside the gridItem) with a dynamically generated id (like "cmb"+i where i is an integer).

I've tried this["cmb"+0], myGrid["cmb"+0], parentDocument["cmb"+0] and each time I'm getting "ReferenceError: Error #1069: Property cmb0 not found on adminUsers and there is no default value" or "ReferenceError: Error #1069: Property cmb0 not found on mx.containers.Grid and there is no default value"

Note : "adminUsers" is the name of my application

Edit : if it can help someone, I've solved my problem a different way. Now I'm filling my grid with a repeater

<mx:Grid id="myGrid">
       <mx:Repeater id="repeater" dataProvider="{lst1}">
        <mx:GridRow>
         <mx:GridItem><mx:Label text="{repeater.currentItem.name}" /></mx:GridItem>
         <mx:GridItem><mx:ComboBox id="cmb" dataProvider="{lst2}" labelField="comment" /></mx:GridItem>
        </mx:GridRow>
       </mx:Repeater>
      </mx:Grid>

Now I can access my combos in a loop with something like :

var combo:ComboBox = cmb[i] as ComboBox;

:)

Olivier
A: 

same question, no answer.. this['myid'] won't work for me.. what am i doing wrong?

jj
A: 

you can use getChildByName("childname")

freak
A: 

I am not able to access object creating using addChild with this['objectName']. Do I need to do anything after addChild so it can be referenced with this?

Andy