Created
February 28, 2017 19:51
-
-
Save liposo/452975a1216ce1cb3cf4d9a4487b80b7 to your computer and use it in GitHub Desktop.
Turbolink-fix Rails 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment