tags:

views:

34

answers:

2

I have a component that has a sub-component they both use a shared variable from the model. The shared variable needs to be set by the parent component before it can be used by the child component. I did like this in the parent component:

<mx:Canvas
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    ...
    creationComplete="group1_completeHandler(event)" >
        ....
        protected function group1_activateHandler(event:Event):void {
           model.myVariable = something;
        }
   ....
   <components:myCustomComponent>
     ...
   <components:myCustomComponent>
 ...
</mx:Canvas>

But for some reason when the code inside myCustomComponent tries to use myVariable for the first time I get a "null" object error. This means I guess that the child component gets rendered before the group1_activateHandler gets called and consequently myVariable gets set.

What should I do to ensure that the parent container initializes the variable before the child component gets created?

A: 

I recommend you factor the variable out of the components into a separate code which you can instantiate separately from the actual components. Then use binding to bind your components to this class. This will give you a much cleaner design.

ilikeorangutans
the variable is actually out of the component it's in the "Model Locator" as I'm using Cairngorm and it's bindable. But I need the parent components to instantiate it first.
Tam
+2  A: 

You should set the variable in initialize() instead of creationComplete() which is invoked after all components are created and rendered.