8 ### via Data Attributes
10 Trigger reflexes without writing any javascript with the `data-reflex` attribute.
17 data-reflex="click->CounterReflex#increment"
19 data-count="<%= @count.to_i %>"
20 >Increment <%= @count.to_i %></a
24 #### counter_reflex.rb
27 class CounterReflex < StimulusReflex::Reflex
29 @count = element.dataset[:count].to_i + element.dataset[:step].to_i
34 ### from Stimulus.js Controller
36 Stimulus.js controllers registered with StimulusReflex can use the `stimulate` method to trigger reflexes
42 data-controller="counter"
43 data-action="click->counter#increment"
44 >Increment <%= @count %></a>
47 #### counter_controller.js
50 import { Controller } from 'stimulus'
51 import StimulusReflex from 'stimulus_reflex'
53 export default class extends Controller {
55 StimulusReflex.register(this)
59 event.preventDefault()
60 this.stimulate('Counter#increment', 1)
65 #### counter_reflex.rb
68 class CounterReflex < StimulusReflex::Reflex
69 def increment(step = 1)
70 session[:count] = session[:count].to_i + step
79 Instead of refreshing the entire page, you can specify a portion of the page to update with `morph(selector, content)`
82 <!-- show.html.erb -->
83 <header data-reflex="click->Example#change">
84 <%= render partial: "path/to/foo", locals: {message: "Am I the medium or the massage?"} %>
89 <!-- _foo.html.erb -->
91 <span class="spa"><%= message %></span>
97 class ExampleReflex < ApplicationReflex
99 morph "#foo", "Your muscles... they are so tight."
106 Use `morph :nothing` in reflexes that do something on the server without updating the client.
110 class ExampleReflex < ApplicationReflex
112 LongRunningJob.perform_later
120 ### Server-side callbacks
122 Reflex classes can use the following callbacks. [Full Docs](http://docs.stimulusreflex.com/lifecycle#server-side-reflex-callbacks)
128 ### Client-side callbacks (generic)
130 StimulusReflex controllers automatically support five generic lifecycle callback methods.
132 - `beforeReflex(element, reflex, noop, reflexId)` prior to sending a request over the web socket
133 - `reflexSuccess(element, reflex, noop, reflexId)` after the server side Reflex succeeds and the DOM has been updated
134 - `reflexError(element, reflex, error, reflexId)` whenever the server side Reflex raises an error
135 - `reflexHalted(element, reflex, noop, reflexId)` reflex canceled with throw :abort in the before_reflex callback
136 - `afterReflex(element, reflex, noop, reflexId)` after both success and error
137 - `finalizeReflex(element, reflex, noop, reflexId)` after both success and error
139 ### Client-side callbacks (custom)
141 StimulusReflex controllers can define up to five custom lifecycle callback methods for each Reflex action. These methods use a naming convention based on the name of the Reflex. e.g. for the `add_one` reflex:
143 - `beforeAddOne(element, reflex, noop, reflexId)`
144 - `addOneSuccess(element, reflex, noop, reflexId)`
145 - `addOneError(element, reflex, error, reflexId)`
146 - `addOneHalted(element, reflex, noop, reflexId)`
147 - `afterAddOne(element, reflex, noop, reflexId)`
148 - `finalizeAddOne(element, reflex, noop, reflexId)`
150 ### Client-side events
152 If you need to know when a Reflex method is called, but you're working outside of the Stimulus controller that initiated it, you can subscribe to receive DOM events
154 - `stimulus-reflex:before`
155 - `stimulus-reflex:success`
156 - `stimulus-reflex:error`
157 - `stimulus-reflex:halted`
158 - `stimulus-reflex:after`
160 There are also events related to the StimulusReflex library setting up and connecting to ActionCable
162 - `stimulus-reflex:connected`
163 - `stimulus-reflex:disconnected`
164 - `stimulus-reflex:rejected`
165 - `stimulus-reflex:ready`
171 If a Reflex is called on a form element - or a child of that form element - then the data for the whole form will be properly serialized and made available to the Reflex action method as the `params` accessor. [Read more](http://docs.stimulusreflex.com/working-with-forms)
175 `stimulate()` method returns a promise
178 this.stimulate('Comments#create')
179 .then(() => this.doSomething())
180 .catch(() => this.handleError())
183 ### Inheriting data-attributes from parent elements
185 You can use the `data-reflex-dataset="combined"` directive to scoop all data attributes up the DOM hierarchy and pass them as part of the Reflex payload.
188 <!-- new.html.erb -->
189 <div data-post-id="<%= @post.id %>">
190 <div data-category-id="<%= @category.id %>">
191 <button data-reflex="click->Comment#create" data-reflex-dataset="combined">Create</button>
198 class CommentReflex < ApplicationReflex
200 puts element.dataset["post-id"]
201 puts element.dataset["category-id"]
208 Instead of updating your entire page, you can specify exactly which parts of the DOM will be updated using the `data-reflex-root` attribute. [Full docs](http://docs.stimulusreflex.com/morph-modes#scoping-page-morphs)
211 <!-- index.html.erb -->
212 <div data-reflex-root="[forward],[backward]">
213 <input type="text" value="<%= @words %>" data-reflex="keyup->Example#words">
214 <div forward><%= @words %></div>
215 <div backward><%= @words&.reverse %></div>
222 @words = element[:value]
226 ### Permanent elements
228 Add data-reflex-permanent to any element in your DOM, and it will be left unchanged by full-page Reflex updates and morph calls that re-render partials.
231 <!-- index.html.erb -->
232 <div data-reflex-permanent>
233 <iframe src="https://ghbtns.com/github-btn.html?user=hopsoft&repo=stimulus_reflex&type=star&count=true" frameborder="0" scrolling="0" class="ghbtn"></iframe>
234 <iframe src="https://ghbtns.com/github-btn.html?user=hopsoft&repo=stimulus_reflex&type=fork&count=true" frameborder="0" scrolling="0" class="ghbtn"></iframe>
238 ### Aborting a reflex
240 call `raise :abort` within a reflex method to cancel it.
244 class CommentReflex < ApplicationReflex