2 Replies Latest reply on Aug 19, 2007 11:39 AM by MikeH883

    complete event on URLLoader issue --newbie question

      Hello. I am an actionscript newbie, and need some help understanding when the complete event fires on URLLoader.

      I have a User class that calls a Server Class. I am expected to populate the User properties with the result set from my server call.

      public function BuildUser(name:String, pw:String, url:String)
      var vars:URLVariables = new URLVariables();

      callServer = new CallServer(url, vars);

      public function initUserProps()
      userXMLList = new XMLList(callServer.getReturnXML());
      userObject = new User();

      The problem is that the complete event on my URLLoader class has not fired before I try to get the values. Here is some of the server class:

      private function sendData( ):void {
      // Create a URLRequest to contain the data to send
      // to process.cfm
      var request:URLRequest = new URLRequest( url );

      // Create name-value pairs to send to the server
      request.data = queryData;

      // Create a URLLoader to send the data and receive a
      // response
      var loader:URLLoader = new URLLoader();

      // Expect the script to return URL-encoded variables
      loader.dataFormat = URLLoaderDataFormat.TEXT;

      // Listen for the complete event to read the server response
      loader.addEventListener( Event.COMPLETE, handleComplete );
      // Send the data in the URLRequest off to the script
      loader.load( request );


      private function handleComplete( event:Event ):void {
      var loader:URLLoader = URLLoader( event.target );

      // Expect the script to return name and description variables.
      // Display these values in a text field on the screen.
      returnXML = new XMLList(loader.data);
      //loginXMLColl = new XMLListCollection(loginXML.root.login);

      and finally here is my mxml:

      <mx:SetEventHandler target="{logonButton}" name="click" handler="validateUser()"/>

      and my function embedded into the mxml:
      public var user1:BuildUser;
      public function validateUser()
      user1 = new BuildUser(userTextInput.text, pwTextInput.text, ' http://local.photoeq-flex.com:8888/php/login-flex.php');

      I have traced through this, and only after the execution leaves the <mx:SetEventHandler /> call does the complete event fire, thus showing my expected result from my server.

      Could someone please point me in the right direction as to what I am doing wrong?

        • 1. Re: complete event on URLLoader issue --newbie question
          MikeH883 Level 1
          I think since Actionscript is asynchronous, I am trying to grab the values from my URLLoader call before it has completed. Since he debugger is not asynchronous in it's representation of how it steps through the code, this is where my confusion is....

          My problem now is, how do I do want I want to do... I know I need to move my processing down to the complete event, but my original object design did not allow for this, since my completeHandler method has no idea what type of data object it was called to populate... I guess should move this data object creation as part of this completeHandler method, and have a factory that will create the correct object?

          Still looking for some suggestions here... Thanks, Mike
          • 2. Re: complete event on URLLoader issue --newbie question
            MikeH883 Level 1
            Can I throw an Event, or somehow bubble it up?