Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Last active May 17, 2020 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopsoft/a218af9d97024e212f749160a9dd351e to your computer and use it in GitHub Desktop.
Save hopsoft/a218af9d97024e212f749160a9dd351e to your computer and use it in GitHub Desktop.
Surgical DOM Updates with StimulusReflex
# 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
<!-- 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