开发者

How to know the order of component rendering in Flex

开发者 https://www.devze.com 2022-12-27 11:37 出处:网络
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 childcomponent. I d

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消