Turbolinks 5.0 and Facebook SDK

前端 未结 3 773
南旧
南旧 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")
    

提交回复
热议问题