1

I'm trying to use a MVC in my JS the problem is that I can't figure out how to obtain the following behaviour.

I have a var granted inside a "member" function (checkFbEmailPermission) of the var/controller fbLoginController , I then make a call to the Facebook API with an anonymous function as parameter. I would like this anonymous function to update the previously declared variable (depending on some condition) but I can't figure out why. I do know that the example does not work because the anonymous function has it's own scope.

Problem is: I can't make the anonymous function return the value (true or false) because it's a callback as parameter function and I can't collect the return value. I don't want to call other functions (if possible) from inside the anonymous function either. I've tried declaring the anonymous function outside (naming it) with two parameters (response and granted) but I can't figure out the scope handling correctly.

So, how would you change the value of the var granted inside function(response){} ?

var fbLoginController = {
  checkFbEmailPermission : function (){

    var granted = false; // VALUE TO BE UPDATED

    FB.api('/me/permissions', function(response) {
      // We get the permissions object contained on the response
      var permissions = response;
      for (var i = 0; i < permissions.data.length; ++i){
        var pm = permissions.data[i];
      // Search for the email permission object
        if (pm.permission == "email"){
        // Check if the email permission is granted
          if (pm.status == "granted"){
            granted = true;  // UPDATING OF THE VALUE
          }
        }
      }
    });
    // Here VALUE of granted should be true if permission is granted
  }
}

0 Answers0