Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook SDK and rails 4 Turbolinks

I'm having a hard time trying to load fast the javascript Facebook SDK into my rails 4 application. Is there a good way to make it work correctly with turbolinks?

if i add this code on my JS application assets. It's not working properly due to turbolinks:

<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
  };

  // 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>

thanks

like image 424
Jonnyx Delavilla Avatar asked Dec 01 '22 20:12

Jonnyx Delavilla


2 Answers

You will find the proper solution for integrating the Facebook SDK with Turbolinks here :

http://reed.github.io/turbolinks-compatibility/facebook.html

like image 104
Pierre Olivier Martel Avatar answered Dec 24 '22 05:12

Pierre Olivier Martel


I was having a problem with the Like Box not loading when I navigate between pages using turbo links. My solution was to create a social.js.coffee in my assets/javascripts folder. In this file it simply has

$(document).on 'page:change', ->
  FB.init({ status: true, cookie: true, xfbml: true });

I know its a lot to put in it's own file ;-), but the idea was that I know google analytics, twitter, and others will have the same conflicts and this will be a nice place to house those solutions as well.

like image 37
Gallonallen Avatar answered Dec 24 '22 04:12

Gallonallen