3 Replies Latest reply on Jun 2, 2009 9:45 AM by tog13

    Accessing client data asynchronously w/HTTPService

    tog13

      I am issuing multiple HTTPService requests inside a loop and want the service's result handler to have access to the loop variable (which is XML) with the value at the time of the request, not at the time of the response (which would be random since HTTPRequest is asynchronous).  For example, my AS code looks like ...

       

      private function iterate {


          // Here's the loop in which I am issuing the HTTPService request.

          // I'd like the result handler to have access to xmlValue's

          // value at the time the request is sent.


          for each (var xmlValue:XML in xmlCollection) {


              // The URL is variable, based partially on data in the loop value's XML

              myService.url = my URL;

              myService.send();

          }

      }



      private function resultHandler(event:ResultEvent):void {


           // I'd like this to be the loop value at the time of the request.

           var xmlValueAtTimeOfRequest:XML = ???; 


           // perform processing based on the above value and the result of the request ...

           achieveHappiness(event.result, xmlValueAtTimeOfRequest);

      }

       

       

      ... and the pertinent MXML is ...

       

      <mx:HTTPService id="myService"
         result="resultHandler(event)"
         resultFormat="e4x"
      />

       

      I've explored

      • adding parameters to the send() method
      • <mx:request>
      • data binding

      but I'm at a loss.  I'm new to Flex and I'm trying to get something turned around quickly w/o resorting to hard-coding all of the XML into an AS file, which would not be a good long-term solution.  From what I've read, using a Timer is not the way to go, either.  This seems like a fairly simple (and common?) problem.  I'm hoping I've just missed the obvious.

        • 1. Re: Accessing client data asynchronously w/HTTPService
          BennyBeta Level 4

          Use an AsyncToken.  One is returned from the send() call.  Just set the data on it and it is accessible in the result handler.

           

          Ben Edwards

          • 2. Re: Accessing client data asynchronously w/HTTPService
            BennyBeta Level 4

            Actually, you'll need to use a Responder:

            http://www.flexafterdark.com/docs/ActionScript-Responder

             

            So in your loop:

             

            myService.url = my URL;

            var token:AsyncToken = myService.send();

            token["xmlValue"] = xmlValue;

            var responder:AsyncResponder = new AsyncResponder( resultHandler, faultHandler );
            token.addResponder( responder );

             

            Then in your result handler:

             

            private function resultHandler( event:ResultEvent, token:Object=null ):void {

            var xmlValueAtTimeOfRequest:XML = token["xmlValue"];

             

            Let me know if that does the trick.

             

            Ben Edwards

            • 3. Re: Accessing client data asynchronously w/HTTPService
              tog13 Level 1

              Actually, the first suggestion alone (using the AsyncToken) seems to have done the trick, though I haven't fully tested it.  In debug mode I can see that the first request resulted in the proper data being sent.  I can't tell you how helpful this is.  I blew pretty much the whole day yesterday trying to sort out a design that would let me get to the right loop data at the right time, but the asynchronicity stopped me every time.  After many years of desktop development I'm trying my hand at client/server and it's a real learning experience.  Now that I have access to the data I can get back to writing the business logic.  Thanks so much!