Setting variables during component initialization in Flex
I am loading a component which makes a HTTPService call to get data that will then be used to set certain variables in the component. I make the HTTPService call in an init() function (for the initialization event) and then set the variables according to the data received in the HTTPService result handler. However, the variables are still null at both the initialize stage and at the creationComplete stage.开发者_运维技巧 If I try and read the variables in a creationComp() function (for the creationComplete event), those variables are still null. Is this correct?
I guess I don't understand the flex initialization cycle very well. When are those variables actually set and available to be used? I need to manipulate those variables automatically after the component loads. Is there an event that comes after creationComplete that is appropriate or some other way to approach this? I am using Flex 3.
Your understanding of the Flex component lifecycle is correct; initialize event fires before creationComplete.
However, an HTTPService call is a separate asynchronous operation. The result handler is not guaranteed to be called by the time the creationComplete event fires. You should do the manipulation of the variables in the result handler instead.
You should think about preventing the creationComplete event being dispatched from your component until the HTTPService has returned, then manually dispatch the event yourself.
That would sort out your timing issues.
精彩评论