tags:

views:

440

answers:

2

In the code below when I call chkAuthentication function from another function
the remoteObj.login (login function in my service file (.php)) is called after the remaining code in that function.
i.e., the loginStatus is returned from the function before the result-handler function loginResult is called. but my loginStatus is supposed to be set in loginResult function. It seems that the asynchronous behaviour is the culprit.
what should I do in order to get the loginResult function to complete first?
Please help me out. Thank you.

 private var loginStatus:Boolean;

  public function chkAuthentication(loginVOObj:LoginVO):String{
                remoteObj.login.addEventListener(ResultEvent.RESULT,loginResult);  
     remoteObj.login(loginVOObj);
   if(loginStatus == true){
    return displayName;
   }
   else{
    return 'fail';
   }
  }

  private function loginResult(result:ResultEvent):void
             {
  if(result.result == null){
   loginStatus=false;
  }else{
   loginStatus=true;

  }

     }
+1  A: 

The chkAuthentication method should not return a String since it is asynchronous. Instead, just create an instance variable and set its String value in the loginResult method. You can then use a binding or dispatch an event to update the UI.

Christophe Herreman
can u please elaborate how can I use binding?
prashant
A: 

The previous answer is correct - rather than depending on the service to act synchronously, which, aside from performance issues, is a rare case in flex, you should use the loginResult function to store the login status in this object or an object that you're using to store the application's state. Then, display it using a databound control:

<mx:label text={userStatus.loginDisplay} />

Dan Monego