views:

31

answers:

2

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.

A: 

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.

Rhysyngsun
Thanks, that makes sense. I did some more testing and the variables would sometimes be set by the time creationComp fires but sometimes not.
Steven
A: 

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.

Gregor Kiddie
How do I know when the HTTPService has returned (do you mean by putting logic in the HTTPService result handler?) and how does one delay the creationComplete event until a certain condition is met (such as HTTPService being returned)?
Steven
The HTTP service dispatches the result event when it's gotten data. http://help.adobe.com/en_US/FlashPlatform//reference/actionscript/3/mx/rpc/http/HTTPService.html#event:result
Gregor Kiddie
The creationComplete event is dispatched by your component in the set initialized method that it inherits from UIComponent. If you override this method and prevent the dispatch of the event here, you can dispatch it in the handler for the http service result event.
Gregor Kiddie
Thanks, that helps to know. I'll probably put the variable setting logic in the HTTP result handler this time since it's a simple operation but good to know this is another way to go about it
Steven