1 Reply Latest reply on Dec 11, 2007 9:35 AM by ntsiii

    Create generic HTTPService class

    Zolotoj Level 3
      I am trying to create a generic HTTPService class. I am using this code as my starting point:
      import mx.controls.Alert;
      import mx.rpc.http.HTTPService;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;

      private var service:HTTPService

      public function useHttpService(parameters:Object):void {
      service = new HTTPService();
      service.destination = "sampleDestination";
      service.method = "POST";
      service.addEventListener("result", httpResult);
      service.addEventListener("fault", httpFault);
      service.send(parameters);
      }

      public function httpResult(event:ResultEvent):void {
      var result:Object = event.result;
      //Do something with the result.
      ?? How to run my function here if I dont know a name of it ??
      }

      public function httpFault(event:FaultEvent):void {
      var faultstring:String = event.fault.faultString;
      Alert.show(faultstring);
      }

      httpResult should be able to fire different functions that a service requester will provide. How can I send a function name or something else for the httpResult to fire when data has arrived?

      Thanks
        • 1. Re: Create generic HTTPService class
          ntsiii Level 3
          There are several options. First look into the AsyncToken. It allows you to associate a dynamic "token" object with a given request and result. properties of this object can be almos anyting, including strings, if you want to use a switch statement to direct the results to the desited handler, and even functions, if you want to specify a callback function.

          Additionally, bracket notation will resolve a function name expressed as a string, and let you call that. So if you pass in a reference to the scope of the handler, say "caller", and the calling component has a public function processResult(), and you pass the name of that function in via a var, say "handlerFunc", then from wiithin your HTTP component you can do:
          caller[handlerFunc](whatever)

          Tracy