2012-03-05 2 views
0

이 코드는 어떻게 다른 함수에서 사용자 ID를 얻고 전역 변수로 사용할 수 있습니까 ??facebook response global을 사용하는 방법은 무엇입니까?

function get_id() { 
     FB.getLoginStatus(function(response) { 
     if (response.status === 'connected') { 
     // the user is logged in and connected to your 
     // app, and response.authResponse supplies 
     // the user’s ID, a valid access token, a signed 
     // request, and the time the access token 
     // and signed request each expire 
     uid = response.authResponse.userID; 
     accessToken = response.authResponse.accessToken; 
     } else if (response.status === 'not_authorized') { 
     // the user is logged in to Facebook, 
     //but not connected to the app 
     } else { 
     // the user isn't even logged in to Facebook. 
     } 
    }); 
    } 

fb_user_id = get_id(); 

답변

0

가장 좋은 방법은 전화를 통해 대신 할당의 함수 내에서이 변수를 설정하는 것입니다 :

(function(w) { 
    FB.getLoginStatus(function(response) { 
     if (response.status === 'connected') { 
      // the user is logged in and connected to your 
      // app, and response.authResponse supplies 
      // the user’s ID, a valid access token, a signed 
      // request, and the time the access token 
      // and signed request each expire 

      w.fb_user_id = response.authResponse.userID; 
      w.accessToken = response.authResponse.accessToken; 
     } else if (response.status === 'not_authorized') { 
      // the user is logged in to Facebook, 
      //but not connected to the app 
     } else { 
      // the user isn't even logged in to Facebook. 
     } 
    }); 
})(window); 

을 그리고 나중에 페이지에 (전화를 제공하는 것이 바로 이것에 의존하지 않는다)을 수행 할 수 있습니다 페이지의 아무 곳에서나 변수 전역 변수로 액세스 fb_user_id (window.fb_user_id.

사용자 ID가 준비되면 바로 코드를 실행해야하는 경우 콜백을 사용해야합니다. jQuery를 사용하는 경우 1.5 당신은 또한 동시성 문제를 돕기 위해 jQuery Deferreds를 사용할 수 있습니다 :

var getUserId = function() { 
    return $.Deferred(function(d) { 
     FB.getLoginStatus(function(response) { 
      if (response.status === 'connected') { 
       // the user is logged in and connected to your 
       // app, and response.authResponse supplies 
       // the user’s ID, a valid access token, a signed 
       // request, and the time the access token 
       // and signed request each expire 
       var auth = response.authResponse; 
       d.resolve(auth.userID, auth.accessToken); 
      } else if (response.status === 'not_authorized') { 
       // the user is logged in to Facebook, 
       //but not connected to the app 

       d.reject(); 
      } else { 
       // the user isn't even logged in to Facebook. 

       d.reject(); 
      } 
     }); 

    }).promise(); 
}; 

getUserId() 
    .done(function(userId, token) { 
     // userId and token are available in here 
    }) 
    .fail(function() { 
     // run some code in here if not authorized 
     // or something else failed; 
    }); 
+0

의 새로운 기능 그럼 어떻게 w.fb_user_id를이 함수에서 사용할 수 있습니까? –

+0

액세스 할 수 없습니다. 나는 "console.log (fb_user_id);"시도했다. 그리고 나는 undifened 오류있어. 이것은 탭 페이지에서 작동하는 페이 스북 응용 프로그램입니다. 영향을 미칩니 까? –

+0

내가 말했듯이이 방법을 사용하면 _asynchronously_로 설정되어있어 사용자 ID에 즉시 액세스 할 수 없습니다. 다른 대안을위한 콜백 메커니즘을 제공 할 것입니다. – Eli

0

JavaScript는 본질 상 비동기입니다. 응답 함수 내에서 다른 함수를 다시 호출해야합니다.

+0

예를 들려 줄 수 있습니까? JS –

관련 문제