Why I hate doLater and waiting a frame

General

The whole method of waiting a frame to allow other functions to complete never sat right with me. It just seems.. broken.
In the MX components, doLater is used a lot - a function for putting off execution of another function until the next frame.. usually to allow other child components to finish initializing before laying them out.

Personally I would have used a cascading series of events, where the child components dispatch an “Ok I’m ready” event. The parent component would listen for these and when all child components are ready, it does its stuff and dispatches its own “Ready” event.

I’d be interested to know what methods other people use to make sure that things like layout get executed in the right order.

This entry was posted on Tuesday, December 20th, 2005 at 12:32 pm and is filed under General. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

9 Responses to “Why I hate doLater and waiting a frame”

  1. Keith Peters Says:

    I agree, it definitely smells like a hack. One of those leftover artifacts from the Flash timeline. As Nigel Pegg is quoted as saying, “If all else fails, wait another frame.”

  2. Mike Jones Says:

    Couldn’t agree more. Bubbled events are an obvious solution to this hack, and something I utilize over doLater as much as I can whilst still allowing for compatability with the v2 framework.

  3. senocular Says:

    doLater is a hack. I don’t like it just as much as the next guy. I usually implement my own callbacks to avoid such trickery.

  4. JesterXL Says:

    For MovieClip’s, use onLoad.

    For Components, use onLoad, or for mx.core.View, use initLayout.

    In Flex 1.5, use childrenCreated.

    In Flex 2, use creationComplete.

  5. nig Says:

    I don’t think I ever said “if all else fails, wait another frame” (I think you might be thinking of Peldi.. and I got on his case about that). If you’re not making components, you should never need doLater. However, there are a couple of great times and places for doLater in building components… They have nothing to do with event bubbling or timelines or all that other random stuff you guys are citing (as Jester points out, we already have events for initialization even in V2). Since everyone here is an expert (especially, it seems, in identifying hacks), can anyone tell me what they are? ;-)

  6. Ash Says:

    I think I came across this while extending mx.core.View. The idea that initLayout came one frame later than I expected messed up my thinking.

    I ask for peoples opinions about this because I happen to be writing my own component framework, and its always nice to know that you’re going about things the right way :)

  7. Peter Says:

    Nigel, I do believe I heard you say or at least quote that “if all else fails … ” in your Q&A session at the last edition of FlashForward in SF ;)

    In any case, I do use doLater quite a lot when doing v2 component development. As was rightly pointed out the components do have their init events and use it for dealing with component params.

  8. Igor Says:

    There is a need for doLater(or postponed execution) when making components. If working with MM v2 components, there is no way to avoid it.

  9. bill Says:

    we need some help with actonscript and flash v2 component knowledge/expertise

Leave a Reply