3 Replies Latest reply on Oct 16, 2008 7:06 AM by Newsgroup_User

    Waiting for event handlers to return

    ks_flex_user Level 1
      I have the following code:

      .......
      public function foo():void {
      loader = new URLLoader();
      loader.addEventListener(Event.COMPLETE, onComplete);
      loader.load(new URLRequest(filename));
      }
      ......

      The caller for foo needs to get some fields that are set by the onComplete function. Since foo returns before onComplete returns, the caller is unable to get the correct value. How do I specify that I want to wait for onComplete to finish before foo() returns?

      Thanks.
        • 1. Re: Waiting for event handlers to return
          atta707 Level 2
          You can't. Done want to either! Let the onComplete do whatever you want the caller of the foo to do. Or simply, dispatch another custom event from onComplete and let the interested party handle it.
          • 2. Re: Waiting for event handlers to return
            ks_flex_user Level 1
            Can you please explain why this is not advisable? I feel that doing it your way would mix, in my case, the model and the view. Here's a simplified version of what I'd like to do: Parse the XML, create an instance of the model (a .as class file). The view then gets updated with the model (from a .mxml file) contents. If I do things as you suggest, I'd have to pass a handle to the view into the onComplete method that's a part of the model. In case I need to update more than once, then your other suggestion of handling a custom event also seems a bit unwieldy: I'd have to keep some flag to know if the event has occured or not, so that the first view update happens in the handler which only gets called once and sets the flag, and the next updates need to rely on the flag being set.

            I'm sure that it has been implemented this way for a very good reason. I'm probably missing the right way to do this all!! Can you please explain it? Thanks!!
            • 3. Re: Waiting for event handlers to return
              Level 7

              "ks_flex_user" <webforumsuser@macromedia.com> wrote in message
              news:gd77k7$6rl$1@forums.macromedia.com...
              > Can you please explain why this is not advisable? I feel that doing it
              > your way
              > would mix, in my case, the model and the view. Here's a simplified version
              > of
              > what I'd like to do: Parse the XML, create an instance of the model (a .as
              > class file). The view then gets updated with the model (from a .mxml file)
              > contents. If I do things as you suggest, I'd have to pass a handle to the
              > view
              > into the onComplete method that's a part of the model. In case I need to
              > update
              > more than once, then your other suggestion of handling a custom event also
              > seems a bit unwieldy: I'd have to keep some flag to know if the event has
              > occured or not, so that the first view update happens in the handler which
              > only
              > gets called once and sets the flag, and the next updates need to rely on
              > the
              > flag being set.
              >
              > I'm sure that it has been implemented this way for a very good reason. I'm
              > probably missing the right way to do this all!! Can you please explain it?
              > Thanks!!

              http://www.adobe.com/devnet/flex/articles/graduating_pt1.html