Turbolinks 5.0 and Facebook SDK

前端 未结 3 774
南旧
南旧 2021-02-11 06:31

Last week I upgraded to Rails 5 which is using Turbolinks 5.0. I used the following script to load the Facebook like button with Turbolinks 3.0:

相关标签:
3条回答
  • 2021-02-11 06:55

    The reasons for this is because some of the events was renamed in Turbolinks 5 and its not compatible with Turbolinks 3. my suggestion is try creating a file under javascripts/ folder called compatibility.coffee

    compatibility.coffee

    {defer, dispatch} = Turbolinks
    
    handleEvent = (eventName, handler) ->
      document.addEventListener(eventName, handler, false)
    
    translateEvent = ({from, to}) ->
      handler = (event) ->
        event = dispatch(to, target: event.target, cancelable: event.cancelable, data: event.data)
        event.preventDefault() if event.defaultPrevented
      handleEvent(from, handler)
    
    translateEvent from: "turbolinks:click", to: "page:before-change"
    translateEvent from: "turbolinks:request-start", to: "page:fetch"
    translateEvent from: "turbolinks:request-end", to: "page:receive"
    translateEvent from: "turbolinks:before-cache", to: "page:before-unload"
    translateEvent from: "turbolinks:render", to: "page:update"
    translateEvent from: "turbolinks:load", to: "page:change"
    translateEvent from: "turbolinks:load", to: "page:update"
    
    loaded = false
    handleEvent "DOMContentLoaded", ->
      defer ->
        loaded = true
    handleEvent "turbolinks:load", ->
      if loaded
        dispatch("page:load")
    
    jQuery?(document).on "ajaxSuccess", (event, xhr, settings) ->
      if jQuery.trim(xhr.responseText).length > 0
        dispatch("page:update")
    
    0 讨论(0)
  • 2021-02-11 07:15

    If you prefer to use native Turbolinks 5 events, you can add this script to your Rails assets:

    // FacebookSDK
    // https://developers.facebook.com/docs/plugins/page-plugin/
    (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.8";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk')); // Replace 'facebook-jssdk' with your page id.
    
    // Compatibility with Turbolinks 5
    (function($) {
      var fbRoot;
    
      function saveFacebookRoot() {
        if ($('#fb-root').length) {
          fbRoot = $('#fb-root').detach();
        }
      };
    
      function restoreFacebookRoot() {
        if (fbRoot != null) {
          if ($('#fb-root').length) {
            $('#fb-root').replaceWith(fbRoot);
          } else {
            $('body').append(fbRoot);
          }
        }
    
        if (typeof FB !== "undefined" && FB !== null) { // Instance of FacebookSDK
          FB.XFBML.parse();
        }
      };
    
      document.addEventListener('turbolinks:request-start', saveFacebookRoot)
      document.addEventListener('turbolinks:load', restoreFacebookRoot)
    }(jQuery));
    

    From: https://gist.github.com/6temes/52648dc6b3adbbf05da3942794b97a00

    0 讨论(0)
  • 2021-02-11 07:19

    Here is a way to integrate Turbolinks 5 with Facebook SDK.

    In your layout template:

    // /source/layouts/layout.erb
    <body class="<%= page_classes %>">
      <%= yield %>
      <div id='permanent' data-turbolinks-permanent></div>
    </body>
    

    Then in your javascript using jQuery here:

    function FBInit() {
      FB.init({
        appId      : 'YOUR_KEY',
        xfbml      : true,
        version    : 'v2.8'
      });
      $('#permanent').append( $('#fb-root').detach() );
    };
    
    $(document).ready(function(){
      $.getScript( "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.8", FBInit);
    });
    
    $(document).on('turbolinks:load', function(event){
      if (typeof FB !== "undefined" && FB !== null) {
        FB.XFBML.parse();
      }
    });
    
    $(document).on("turbolinks:before-cache", function() {
        $('[data-turbolinks-no-cache]').remove();
    });
    

    Then use any Facebook plugins using the data-turbolinks-no-cache attribute like this:

    <div data-turbolinks-no-cache 
      class="fb-like" 
      data-href="#" 
      data-layout="standard" 
      data-action="like" 
      data-size="small" 
      data-show-faces="true" 
      data-share="true"></div>
    

    Here is the gist and here is a blog post explaining how it works

    0 讨论(0)
提交回复
热议问题