Meteor- Facebook社交插件无法加载,直到浏览器刷新

时间:2015-08-03 07:05:49

标签: javascript facebook meteor facebook-like facebook-social-plugins

我在Facebook社交插件方面遇到问题。之类的&我的网站中的评论框仅在刷新浏览器后出现。有谁知道为什么会发生这种情况以及如何解决这个问题?我使用Meteor构建我的网站,并且只有在用户登录后才能访问放置社交插件的页面。非常感谢! :)

测试网站为www.sgeasyaidtest.meteor.com,社交插件位于规划页面下。

这是FB登录&我在标签下放置的社交插件:

<body>

  {{#if currentUser}}

  <!--FB login-->
  <script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '1027408483945691',
      xfbml      : true,
      version    : 'v2.4'
    });
  };

  (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/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
  </script>
  <div
  class="fb-like"
  data-share="true"
  data-width="450"
  data-show-faces="true">
  </div>
  <script>
  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
    }
  }

  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  window.fbAsyncInit = function() {
  FB.init({
    appId      : '692361547558190',
    cookie     : true,  // enable cookies to allow the server to access
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.2' // use version 2.2
  });

  // Now that we've initialized the JavaScript SDK, we call
  // FB.getLoginStatus().  This function gets the state of the
  // person visiting this page and can return one of three states to
  // the callback you provide.  They can be:
  //
  // 1. Logged into your app ('connected')
  // 2. Logged into Facebook, but not your app ('not_authorized')
  // 3. Not logged into Facebook and can't tell if they are logged into
  //    your app or not.
  //
  // These three cases are handled in the callback function.

  FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });

  };

  // 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/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

  // Here we run a very simple test of the Graph API after login is
  // successful.  See statusChangeCallback() for when this call is made.
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }
  </script>

</div>
<!--FB 'like' -->
<div id="fb-root"></div>
<script>(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/sdk.js#xfbml=1&version=v2.4&appId=1027408483945691";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

</script>

<!-- FB comment plugins -->
<div id="fb-root"></div>
<script>(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/sdk.js#xfbml=1&version=v2.4&appId=1027408483945691";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

这是我在页面中放置的代码,我想要社交插件出现。

<!-- 'like & share' plugin -->
<div class="fb-like" data-href="http://www.facebook.com/sgeasyaid" data-layout="button" data-action="like" data-show-faces="true" data-share="true"></div>
<!-- "comment" plugins -->
<div class="fb-comments" data-href="https://www.facebook.com/sgeasyaid" data-width="500" data-numposts="2"></div>

1 个答案:

答案 0 :(得分:2)

不要在HTML正文中包含<script>标记。由于Meteor的反应性,Meteor使用复杂的模板系统,可能会修改DOM。

因此,您应该将外部脚本放在模板事件中,例如Template.myTemplate.onRendered函数。但是,我建议您使用包含Facebook SDK的biasport:facebook-sdk

您可以在Meteor应用程序中包含社交插件,如下所示:

  1. 运行meteor add biasport:facebook-sdk以安装提到的包。
  2. 初始化SDK:
  3. if(Meteor.isClient) {
      window.fbAsyncInit = function() {
        FB.init({
          appId: 'your-app-id', // Specify your app id here
          status: true,
          xfbml: true,
          version: 'v2.1' // Specify your version here
        });
      };
    }
    
    1. 在Meteor模板中包含所需的社交插件,例如:
    2. <template name="facebookLikeButton">
          <div class="fb-like" data-href="http://www.facebook.com/sgeasyaid" data-layout="standard" data-action="like"
               data-show-faces="true" data-share="true"></div>
      </template>
      

      您还可以将built-in template helpers用于社交插件。