R – Flex 3: Data assignment to a subcomponent fails in an MXML component in the action script block

actionscriptapache-flexflex3mxmlvariable-assignment

I'm trying to propagate an assignment to the data parameter of a sub-component through it's parent component's setter. Like this:

<CustomComponent
     xmlns:mx="http://www.adobe.com/2006/mxml"
     xmlns="components.*"
    >
    <mx:Script>
        <![CDATA[
            public override function set data(val:Object):void
            {
                super.data = val;

                subComponent.data = val; //ref #1
            }
        ]]>
    </mx:Script>
    <CustomSubComponent id="subComponent"
        />
</CustomComponent>

When I ran my application, the sub-component never received its data. When debugging and stepping to the line marked "ref #1", the debugger jumps out of the method and continues on to something else as if the method was complete. It seems like some exception or error was thrown but the console gives no indication of what is wrong with this assignment.

Am I doing something stupid here? It seems pretty straight forward.

Environment: This is using Flex SDK 3.2, with the Flex Builder 3 plugin for Eclipse on Windows, with Flash 9 Debug ver. for IE7.

Note: With this particular example I'm trying to avoid Binding on purpose. I mean, why can't I manually push the data to the sub-component rather than binding it?

Best Answer

Flex suppresses errors inside of data setters, because stuff is frequently null. The problem is probably that your subcomponent is not initialized when set data is called. If you really can't use binding (the easy and clean way), then you should delay setting the data of the subcomponent until it is initialized with invalidateProperties / commitProperties.

In the data setter, call invalidateProperties() and set a flag saying they've changed.

Override commitProperties (don't forget to call super), check the flag, and if it's true, set the subcomponent's data.

If your commitProperties block never gets called, try invalidateProperties on creationComplete.