【问题标题】:Meteor- Facebook social plugin cant load until browser refreshesMeteor-Facebook 社交插件在浏览器刷新之前无法加载
【发布时间】:2015-08-03 07:05:49
【问题描述】:

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

测试网站是www.sgeasyaidtest.meteor.com,社交插件在planner页面下。

这是我放在标签下的 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>

【问题讨论】:

  • 这不是 Meteor 方式 来做这件事的。您绝对应该使用模板事件。我建议您将代码放在Template.myTemplate.onRendered 函数中。
  • 你的意思是把

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


【解决方案1】:

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

因此,您应该将外部脚本放在模板事件中,例如Template.myTemplate.onRendered 函数。不过,我建议您使用 biasport:facebook-sdk,它封装了 Facebook SDK。

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

  1. 运行meteor add biasport:facebook-sdk 以安装上述软件包。
  2. 初始化 SDK:

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 模板中包含所需的社交插件,例如:

<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 用于社交插件。

【讨论】:

  • 谢谢,你为我节省了这么多时间@lblessedf 请将答案标记为正确的,请
猜你喜欢
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 2013-12-19
  • 1970-01-01
相关资源
最近更新 更多