A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Dispatching new event upon sending request to HTTPService

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Posts
    7

    Dispatching new event upon sending request to HTTPService

    hi all..
    i've a Services class...which handles the HTTPService requests and responses and faults. Now i want to dispatch a new event whenever i send request to the server...so that i can take custom actions like showing some popup window or disabling certain components...but i'm not able to figure out a way to do it...i'm sure that it is quite simple for most of you..but i'm just not able to figure a way to do it...here is my services class:
    PHP Code:
    package business
    {
    import mx.controls.Alert;
    import mx.managers.CursorManager;
    import mx.rpc.AsyncToken;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    import view.Privileges_Form;

        public class 
    Services
        
    {
            private const 
    ENDPOINT_URL:String "../getPrivilegeDetails.php";
            private static var 
    servicesLocator:Services;
            private var 
    gateway:HTTPService;
            
            
             public static function 
    getInstance() : Services 
              
    {
                  if ( 
    servicesLocator == null )
                  {
                      
    servicesLocator = new Services();
                  }
            
                  return 
    servicesLocator;
              }
              
              public function 
    Services()
                {
                
                if ( 
    servicesLocator != null )
                 {
                     throw new 
    Error"Only one Services instance should be instantiated" );    
                 }
            
    Privileges_Form.
        
                
    gateway=new HTTPService();
                
    gateway.resultFormat="e4x";
                
    gateway.url ENDPOINT_URL;
                
    gateway.method "POST";
                
    gateway.useProxy false;
                
    gateway.addEventListener(ResultEvent.RESULTresultHandler);
                   
    gateway.addEventListener(FaultEvent.FAULTfaultHandler);
             }
              
              public function 
    doRequest(method_name:Stringparameters:Objectcallback:Function):void
            
    {
                
    CursorManager.setBusyCursor();
                
                
    // add the method to the parameters list
                
    parameters['method'] = method_name;
            
                
    gateway.request parameters;
            
                var 
    call:AsyncToken gateway.send();
                
    call.method_name=method_name;
                
    call.request_params gateway.request;
            
                
    call.handler callback;
            }
             private function 
    resultHandler(e:ResultEvent):void
            
    {
                
    CursorManager.removeBusyCursor();
                
                if(
    e.result.data.elements("error").length()>0)
                {
                    
    errorMessageHandler(e);
                }
                else
                {
                    
    e.token.handler.call(null,e);
                }
                   
            } 
            
            private function 
    faultHandler(e:FaultEvent):void
            
    {
                  
    CursorManager.showCursor();
                var 
    errorMessage:String "Connection error: " e.fault.faultString
                if (
    e.fault.faultDetail
                { 
                    
    errorMessage += "\n\nAdditional detail: " e.fault.faultDetail
                } 
                
    Alert.show(errorMessage);
               
    gateway.cancel(); 
               
    CursorManager.removeBusyCursor();
            }
            
            private function 
    errorMessageHandler(e:ResultEvent):void
            
    {
                
    Alert.show("Error Occured:"+e.result.data.error);
                
    CursorManager.removeBusyCursor();
            }

        }

    the method doRequest calls the HTTPServices send method...so in this method, i want to dispatch a new event and handle it in the main application...
    can someone please help me in solving this problem..??
    thanks in advance...

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Posts
    588
    Just make sure your class extends EventDispatcher, then you can dispatch events like:

    dispatchEvent(new Event("someEvent"));

    Or instead of using Event, you can create your own custom event and dispatch that if you need to send additional info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center