Last active
June 29, 2017 01:53
-
-
Save danott/63845616463d79170228ba4018bc8756 to your computer and use it in GitHub Desktop.
An idea for page-specific sprinkles that automatically setup and teardown. Doubling down on Server Rendered JavaScript...
This file contains hidden or 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
class Sprinkle | |
run: => | |
document.addEventListener("turbolinks:load", @setup) | |
setup: => | |
console.log "Sprinkle setup" | |
document.removeEventListener "turbolinks:load", @setup | |
document.addEventListener "turbolinks:before-cache", @teardown | |
teardown: => | |
console.log "Sprinkle teardown" | |
document.removeEventListener "turbolinks:before-cache", @teardown | |
class RootSprinkle extends Sprinkle | |
setup: => | |
super | |
console.log "RootSprinkle setup" | |
element = document.getElementById "main" | |
element.addEventListener "click", -> | |
console.log("clicked <%= params[:id] %>") | |
(new RootSprinkle).run() |
This file contains hidden or 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
module ApplicationHelper | |
def sprinkles(action) | |
javascript_tag do | |
render(partial: "#{action}.js.coffee").html_safe | |
end | |
end | |
end |
This file contains hidden or 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
<%= sprinkles("show") %> | |
<div> | |
<%= link_to "Root #{Time.current.to_i}", root_path(id: Time.current.to_i) %> | |
</div> | |
<div id="main"> | |
Main id: <%= params[:id] %> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment