Here I have:
$(document).on 'turbolinks:load', ->
console.log "page has loaded!"
$ ->
$("button#show-hide").click (e) ->
e.preventDefault()
which makes my click
event available only after Turbolinks caches page. I can do $(document).on 'turbolinks:before-visit
which will make event available only before Turbolinks caches. How should I make event available at all times?
I think the problem here is that you are binding to two events:
turbolinks:load
and$(document).ready()
.Turbolinks does not discard event listeners on the
document
so you can safely bind click handlers to it and they will be called scross page loads. From the Turbolinks documentation:With this in mind, you can do:
How about you simply take the click event out of the 'turbolinks:load' and
'turbolinks:before-visit' context.
or If the button is added dynamically in the DOM, what you can do is attach a click handler to body and on click determine which element was clicked and proceed accordingly.
In coffee script, you can do
UPDATE
From the answer on Rails, javascript not loading after clicking through link_to helper
If you want your click handlers to work on
page:change
and as well as onready
event you can do,