How to insert a Flex component before adding it to the display

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

Is it possible to iterate over MyView and get all information about its children (types, ids ...) before displaying it.

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 is this call not working?

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

It only works if I add a statement like AddChild (MYV); before calling iterateOverChildren. (But that's not what I want, I want to iterate over 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 lifecycle independent, it would allow me to peek into the component without adding a display. What am I missing? How to preview a component before displaying.

+2


source to share


1 answer


Flex provides several ways to create instances of a component.

If you use a constructor in ActionScript, you end up with an empty metal object where nothing has been completed except for the construction of the object itself. In particular, this new object hasn't created its child views yet, so you don't see anything when you look at the results getChildren()

.



If you write your component in MXML, the MXML compiler generates ComponentDescriptors instead of "real" objects. They contain all the information specified for an object in MXML (properties, bindings, event handlers, etc.), and the runtime uses them to create real objects at the appropriate time. "Relevant time" usually means "when the item is added to the display list." This is why you only see the children after the call addChild()

(technically, not immediately after the addChild () call, but only after your newly created object dispatched the creationComplete event).

+2


source







All Articles