2 Replies Latest reply on May 15, 2007 2:12 PM by M40

    Event Dispatch

    M40
      I have this simple Flex / ActionScript function that sends data to a PHP file. Then the PHP file parses the received data and returns another data to the Flex function. What I'd love to know is how to make this simple function dispatch a simple event when it complete loading data from the PHP file.

      The function looks like that:

      public static function myFunction(postData:String):void {

      var _URLRequest:URLRequest = new URLRequest()

      _URLRequest.url = "http: //www.site.com/file.php";
      _URLRequest.data = "data="+postData;
      _URLRequest.method = URLRequestMethod.POST;

      var _URLLoader:URLLoader = new URLLoader(_URLRequest);

      _URLLoader.addEventListener("complete", dispatchCompleteEvent);

      }

      I hope you could help me with some idea!

      Thank you very much!
        • 1. Re: Event Dispatch
          ashar2005
          Use an RPC call so you can send and get data, use an <mx:HTTPService>
          tag with a result="resultHandler(event)" so the results can be handled
          and you can send your dispatchEvent. Example below....

          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" >

          <mx:Script>
          <![CDATA[
          import mx.rpc.events.ResultEvent;
          import mx.rpc.http.HTTPService;


          public function sendData(postData:String):void {

          var s:Object = new Object();
          s.data = postData;
          myHTTPService.send(s);
          }

          private function resultHandler(event:ResultEvent):void
          {

          var myResults:Object = event.result;
          trace(myResults);
          var o:Event = new Event("gotMyResults");
          dispatchEvent(o);

          }


          ]]>
          </mx:Script>

          <mx:HTTPService id="myHTTPService"
          url=" http://www.site.com/file.php"
          result="resultHandler(event)"/>

          </mx:Application>
          • 2. Re: Event Dispatch
            M40 Level 1
            Thank you for your comment ashar2005!

            Though that seems not to be what I'm looking for. I actually can send and get data with the function I wrote. I can access the loaded data from the URLLoader object. Using URLLoader I can also use "complete" event, dispatched when the data is loaded.

            The thing is I need the function to dispatch its own event when the data is loaded by the URLLoader object.

            Now I realize that I had to create this topic in the ActionScript section so I'm sorry about that.