Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

jquery - Facebook Javascript API , callback function?

I am using Facebooks Javascript API in my jQuery Mobile Website in order to import the albums and photos of a facebook page.

Its fairly easy to use the API :

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : 'YOUR_APP_ID',                        // App ID from the app dashboard
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
      status     : true,                                 // Check Facebook Login status
      xfbml      : true                                  // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
    FB.api('16907099963/albums', checkForErrorFirst(getAlbums));
  };

  // Load the SDK asynchronously
  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

As you see i make a call to the API here : FB.api('16907099963/albums',checkForErrorFirst(getAlbums)); which will give me all the albums of this page. The call is asynchronous. I need to find a way to be able to know when the call has finished. But i really cant find any callback functions here..

The API can be found here

The reason i am asking for this , is that i need to add a jquery mobile "loading" widget , before i call the API and make it stop when the albums have rendered. Any idea on this one?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The checkForErrorFirst is the callback function. I think your are also missing a slash before the page ID in FB.api. Quick sollution if you can't edit the checkForErrorFirst function:

loader.show();
FB.api('/16907099963/albums', function(result) {
  loader.hide();
  checkForErrorFirst(getAlbums)(result);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...