2 Replies Latest reply on Sep 29, 2010 7:59 AM by Alex-Wright

    Return ResultEvent properties of HTTPServices inside Class methods...

    Alex-Wright

      Hi,

       

      I currently have my own class, for arguments sake, lets say it's called User and is for logging into my own system.

       

      I would like to call a function from the user class called, say, login(), which would send the username and password attributes to a service (in my case, PHP), and then get the data back with the response (either successful or unsuccessful).

       

      This would ideally make my code behave like this:

       

      var user1:User = new User("Jim", "password");
      var loggedIn:Boolean = user1.login();
      
      

       

      The only problem is that the only way I know how to do this is by using HTTPServices and adding eventListeners, and so if I make the login method in the class:

       

      var loginSuccess:Boolean;
      
      
      
      public function login():Boolean {
           loginSuccess = false;
      
           
      
           var s:HTTPService = new HTTPService;
           s.addEventListener(ResultEvent.RESULT, loginCheck);
      
      
      
      
      
           s.url = "http://.../...php?method=login" +
                          "&username=" + this.username +
                          "&password=" + this.password";
      
      
           s.send();
      
      
           return loginSuccess;
      
      
      
      }
      

       

      The function assigns the loginCheck function to the HTTPService as an EventListener, so the loginSuccess boolean will be returned before it has been changed by the loginCheck function.

       

      private function loginCheck(e:Event):void {     
      
      
      
           if(e.result.login == "successful") {
                loginSuccess = true;
      
           } else {
                loginSuccess = false;
           }
      
      
      
      }
      

       

      I don't know of a way of getting the login function to return a boolean based on the result of the HTTPservice it sends, so does anyone know of a better way of doing this?

       

      Thansk in advance,

       

        Alex.

        • 1. Re: Return ResultEvent properties of HTTPServices inside Class methods...
          Gregory Lafrance Level 6

          The result of login success or failure should be returned in the loginCheck() function after the HTTPService result has been processed.

           

          HTTPService is asynchronous, so you can't do anything expecting the result to have been returned already. Must do it in the result listener after result has been processed.

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex Training and Support Services

          1 person found this helpful
          • 2. Re: Return ResultEvent properties of HTTPServices inside Class methods...
            Alex-Wright Level 1

            Thanks Greg,

             

            so, in terms of having these functions within my class, would it therefore be impossible to call the login as one class function? i.e.

             

            var loggedIn:Boolean = user1.login();

             

            Since the login function would rely on the eventListener's function, I'm guessing not.

             

            I would like to be able to call the login method using this very simple line of code, but if I that can't be done, is there any way I can keep the login and loginCheck functions within the User class? I guess a global variable of loggedIn:Boolean could be set in the class, and then outside of the class, I could set a change event listener to it?

             

            Thanks again,

             

              Alex.