Last active
May 17, 2020 15:05
-
-
Save hopsoft/a218af9d97024e212f749160a9dd351e to your computer and use it in GitHub Desktop.
Surgical DOM Updates with StimulusReflex
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
# app/reflexes/examples_reflex.rb | |
class ExamplesReflex < ApplicationReflex | |
include CableReady::Broadcaster | |
before_reflex :surgical_update, only: [:noop] | |
def surgical_update | |
cable_ready["example"].inner_html( | |
selector: "#example-container", | |
html: ExampleController.render( | |
partial: "/examples/list_item", | |
assigns: {item: Item.find(element.dataset[:id])} | |
) | |
) | |
cable_ready.broadcast | |
throw :abort | |
end | |
def noop | |
# we will never hit this because we throw :abort in before_reflex | |
end | |
end |
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
<!-- app/views/examples/show.html.erb --> | |
<a href="#" data-reflex="click->ExampleReflex#noop">Fetch Data</a> | |
<div id="example-container"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment