tags:

views:

306

answers:

3

Well, not literally, of course, but:

I'm new to Flex and I'm trying to figure out where to put the code that I want to run when my app starts. In my example, I have a tree control defined in the markup, and I have AS code that can populate it from a local file or a webservice. How do I bind the one to the other such that when the control is created, the code is run?

The examples I have found in the documentation all use event handlers, i. e. they put 'creationComplete="myInitFunction(evt)"' in the control's opening tag. That would mean I write a little glue function that initializes things.

Now, as I have understood it, the Markup is really converted into AS classes internally. So can I not just define a class that is instantiated by the engine and put my startup code in the constructor? Maybe even like, extending the Tree class?

EDIT:

Months later, I'm a lot wiser. After trying to abuse the constructors of extended component classes, I had to learn that Flex doesn't expect the constructors to do much - certainly not loading data off the network.

The component life cycle explanation in "Programming Flex 3" was really helpful. Now, my constructors are all empty, and I'm overriding the life cycle methods, such as createChildren for a lot of things, and use events for anything that involves latency or depends on externalities, especially network stuff.

For example, I use creationComplete to initiate an asynchronous network request (using HTTPService), when that returns there are ResultEvent and FaultEvent handlers, the ResultEvent updates a data model to which I data-bound my controls. This is seemingly very Flex-y and works great.

A: 

You should be looking to handle a startup event.

I am not real 'up' on Flex, but according to http://www.wietseveenstra.nl/blog/2007/02/understanding-the-flex-application-startup-event-order:

applicationComplete() dispatches events after the Application has been initialized, processed by the LayoutManager and added to the display list. This is the last event dispatched during an application startup. It is later than the application’s creationComplete() event, which gets dispatched before the preloader has been removed and the application has been attached to the display list.

Geoffrey Chetwood
+3  A: 

The creationComplete event (on the Application object) is dispatched when the Application component is finished being created. The applicationComplete event is dispatched once the entire application is complete (layout, etc.) and is visible to the user (loading screen is finished).

For the situation you mention -- where you're populating a control at startup -- I'd suggest using the creationComplete event of the component containing the control you want to populate (the Tree in your case), which sounds like it'd be your Application component. This is generally a good practice as often this code will depend on sibling components of the one you're populating (e.g., an HTTPService component).

You're right that all MXML is transformed in AS3 which is then compiled. There's a compiler flag that will preserve the AS3 that's generated (it'll be read-only, though). You don't want to mess with that. And due to the (somewhat) complex nature of the UIComponent instantiation sequence (constructor, styling, layout, etc.) it is definitely best practice to populate controls in a creationComplete event (or similar).

Troy Gilbert
Thanks for the insights, Troy. I just winded up using class extension and that seems a pretty neat solution to me. Could you please comment on my answer if the instantiation sequence might mess with that? Are there any docs on the guts of that process?
Hanno Fietz
Months later, I'm somewhat wiser - using events all over now. creationComplete triggers a HTTP request, the returning result triggers update of a data model to which I data bound the control. This seems to be very Flex-y and works great.
Hanno Fietz
+1  A: 

Technically speaking, int main() is analagous to the constructor of the application class in actionscript 3. What this means for Flex is that whatever is the root node of the application mxml file (generally mx:Application), the constructor for that class is your int main().

But like other posters have mentioned, what you want to do is listen for one of the events broadcast by mx:Application. Initialize is a good place to put initalization logic, load configuration files or do anything else you need to do that does not rely on any UI objects or anything that hasn't been created yet. Otherwise creationComplete is the next most likely even that you would want to listen for.

You really shouldn't need to use the applicationComplete event.

Ryan Guill