views:

49

answers:

1

Hi Guys,

I have 3 javascript methods.

isLogin();
login();
reply();

when I'll execute the reply method then first of all it should check that user is Login or not. If user is login then execute the reply method. If user is not login then execute it first and then execute reply method.

These are all Facebook Javascript SDK methods which are asynchronous. Therefore they don't return a value.

if for instance these are not async methods then these can return value, on which I can take decesion like this.

function isLogin(){
if(user.islogin()){
return true
}
else{
return false;
}

function login(){
if(user.hasLogin()){
return true;
}
else{
return false;
}

function reply(){

if(!isLogin(){  //User is not login
   if(login()){ //Login User
      //user has login. now reply
     sendText(); //any statement which will reply
   }
}

}

Please help

Thanks

+1  A: 

I don't have experience with the Facebook JavaScript API, but from looking over their documentation briefly, the FB.getLoginStatus and FB.login methods do in fact return values.

Asynchronous methods can return values, they're just performed in a "non-blocking" manner (the main program flow doesn't halt to wait for them to complete). That's what JavaScript "callback" functions are used for. Your script can continue doing other things after making the API request, and the callback function will get fired when the API returns it's response.

FB.getLoginStatus(function(response) {
  if (response.session) {
    // logged in and connected user, someone you know
  } else {
    // no user session available, someone you dont know
  }
});

The inner function (starting with function(response)) is the callback function, which will fire when the Facebook API returns it's response to your script.

Calling the FB.login method will pop up a login window to the user. Your page will continue functioning while this window is open (as it is asynchronous to your program), and the FB.login callback function will fire once the user has either logged in or cancelled the login process.

For more explanation of how to use the Facebook JavaScript API methods, it's always useful to read the documentation.

Brad
Thanks Brad for your help. I've written these methods as Facebook given in their documentations. But I don't know how will I manage these functions together with callback functions. Please give me a rough idea.
EarnWhileLearn
Well, I would assume the *//no user session available* section above is where you would want to insert your *FB.login* logic, but I can't write your whole application for you. :)
Brad