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 = somet开发者_StackOverflowhing;
}
....
<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?
You should set the variable in initialize()
instead of creationComplete()
which is invoked after all components are created and rendered.
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.
精彩评论