2
votes

Suppose you have a MyView.mxml file, which is basically a Panel with several children (Form, FormItems, Buttons...).

Is it possible to iterate over the MyView and get all the information about its children (types, id ...) before it is displayed.

In my Main.mxml if I have this function

    public function iterateOverChildren(comp:Container):void {
        // Get the number of descriptors.
        trace("Running iterateOverChildren for " + comp.id);
        if (comp != null)
        {
        var n:int = comp.childDescriptors.length;
        for (var i:int = 0; i < n; i++) {
            var c:ComponentDescriptor = comp.childDescriptors[i];
            var d:Object = c.properties;

            // Log ids and types of objects in the Array.
            trace(c.id + " is of type " + c.type);

            // Log the properties added in the MXML tag of the object.
            for (var p:String in d) {
                trace("Property: " + p + " : " + d[p]);
            }
        }
        }
    }

Why does this call not work ?

var myV = MyView(); iterateOverChildren(myV);

It only works if I add a statement like addChild(myV); before the iterateOverChildren call. (But that's not what I want, I want to iterate the descriptions without adding it to display).

When I read this http://livedocs.adobe.com/flex/3/html/help.html?content=layoutperformance_06.html

I thought the "childDescriptors" method is independent from the life cycle, it would let me introspect the component without adding to display. What did I miss ? How do I introspect a component before displayed.

1
Why exactly are you trying to iterate over all of MyView's children? Depending on what information you're looking for, there are multiple ways of doing it. - Simon
I have this meta-data for each item (children) in the back-end per user role. I used to bring all the meta-data but now the application has becoming larger and want to bring down only the related meta data from the back-end. I want to write something dynamic to find all these 'id's (that's my key for meta data) say from MyView before I instantiate. I know getChildren results depends on the creationPolicy, so I when I've read about childDescriptors, it looked like the thing I was looking for. - Alex E. Oscar

1 Answers

2
votes

There are multiple ways of component instantiation in Flex.

If you use the constructor in ActionScript, you get a bare metal object where nothing but construction of the object itself has been completed. In particular, this new object has not yet instantiated its child views, that's why you don't see anything when you look at the results of getChildren().

If you write down your component in MXML, the MXML compiler creates ComponentDescriptors instead of "real" objects. These hold all information you specified for the object in MXML (properties, bindings, event handlers etc), and the runtime uses them to create real objects at the appropriate time. "Appropriate time" usually means "when the object is added to the display list". That's why you only see children after calling addChild() (technically, not immediately after calling addChild(), but only after your newly instantiated object has dispatched its creationComplete event).