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
325 views
in Technique[技术] by (71.8m points)

javascript - Facebook comments load inconsistently with Angular Fullstack

I have FB comments and the FB share button implemented on a blog post page. The FB comments rarely load the first time I come to my blog post page. When I refresh the page one of five things happen: 1) the page loads without comments (most of the time); 2) the page loads with comments; 3)FB is not defined appears; and once in a while I get 4) init not called with valid version. If I refresh twice quickly sometimes the page won't refresh at all and will just return a blank page. In search of this solution I looked at pretty much all of the posts on stack overflow related to this talk including: facebook comments plugin showing only after refresh

I don't know what turbolinks are but I think it has something to do with RoR and this is an Angular Fullstack project. In my index.html:

<body ng-app="theApp">   
    <div id="fb-root"></div>
    <script>

        window.fbAsyncInit = function() {
          FB.init({
            appId      : 'MyApp',
            xfbml      : true,
            version    : 'v2.5'
          });
        };

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

On my comments.html:

<div class="fb-comments" data-href={{webAddress}} data-width="100%" data-numposts="10"></div>

On my comments.controller:

angular.element(document).ready(function() {
      FB.XFBML.parse();
   });

This solution makes my FB share button come up without a refresh (my most recent issue) but it leaves me without my comments appearing until after an additional refresh.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

After several more experiments I finally got the comments to come up on the first page load. There is a lagging issue where if I go to another page (like our social media) then hit backspace quickly it says "FB is not defined". Bonus points for anyone that leaves a better answer. Here is the code that I found that got the page loading the first time:

My index.html:

<body ng-app="theApp">   
    <div id="fb-root"></div>
    <script>

    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'MyApp',
        xfbml      : false, //this was true before,
        version    : 'v2.5'
      });
    };

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

My comments.html:

<script>
  FB.XFBML.parse();
</script>
<div class="fb-comments" data-href={{webAddress}} data-width="100%" data-numposts="10"></div>

My comments.controller.js:

angular.element(document).ready(function() {
      FB.XFBML.parse();
   });

I'm glad it 'works' but given the redundancy of FB.XFBML.parse() in the comments.html and comments.controller.js and the fact that the page will crashes when you hit back after going to another page to fast I find myself unsatisfied. If someone has a better answer, I'd love to hear it.


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

...