2012-03-05 5 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); 

その後、後に自分のページ内(呼び出しはすぐにこの1に依存していない提供)、することができますページ上の変数にグローバル変数fb_user_idwindow.fb_user_id,

とも呼ばれます)を使用してコードにアクセスします。 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);しようとしました"と私は間違ったエラーを得た。これはタブページで動作するFacebookブックアプリです。これは何にも影響しますか? –

+0

私が言ったように、このメソッドを使用すると、_asynchronously_に設定されているため、すぐにユーザーIDにアクセスすることができなくなります。私は別の代替のためのコールバックメカニズムを提供します。 – Eli

0

JavaScriptは本質的に非同期です。応答関数内の別の関数にコールバックする必要があります。

+0

例を挙げることができますか?私はJSの新しいです –