8 ### via Data Attributes
10 Trigger reflexes without writing any javascript with the `data-reflex` attribute.
13 <!-- index.html.erb -->
16 data-reflex="click->CounterReflex#increment"
18 data-count="<%= @count.to_i %>"
19 >Increment <%= @count.to_i %></a
25 class CounterReflex < StimulusReflex::Reflex
27 @count = element.dataset[:count].to_i + element.dataset[:step].to_i
32 ### from Stimulus.js Controller
34 Stimulus.js controllers registered with StimulusReflex can use the `stimulate` method to trigger reflexes
37 <!-- index.html.erb -->
39 data-controller="counter"
40 data-action="click->counter#increment"
41 >Increment <%= @count %></a>
45 // counter_controller.js
46 import { Controller } from 'stimulus'
47 import StimulusReflex from 'stimulus_reflex'
49 export default class extends Controller {
51 StimulusReflex.register(this)
55 event.preventDefault()
56 this.stimulate('Counter#increment', 1)
63 class CounterReflex < StimulusReflex::Reflex
64 def increment(step = 1)
65 session[:count] = session[:count].to_i + step
74 Instead of refreshing the entire page, you can specify a portion of the page to update with `morph(selector, content)`
77 <!-- show.html.erb -->
78 <header data-reflex="click->Example#change">
79 <%= render partial: "path/to/foo", locals: {message: "Am I the medium or the massage?"} %>
84 <!-- _foo.html.erb -->
86 <span class="spa"><%= message %></span>
92 class ExampleReflex < ApplicationReflex
94 morph "#foo", "Your muscles... they are so tight."
101 Use `morph :nothing` in reflexes that do something on the server without updating the client.
105 class ExampleReflex < ApplicationReflex
107 LongRunningJob.perform_later
115 ### Server-side callbacks
117 Reflex classes can use the following callbacks. [Full Docs](http://docs.stimulusreflex.com/lifecycle#server-side-reflex-callbacks)
123 ### Client-side callbacks (generic)
125 StimulusReflex controllers automatically support five generic lifecycle callback methods.
127 - `beforeReflex(element, reflex, noop, reflexId)` prior to sending a request over the web socket
128 - `reflexSuccess(element, reflex, noop, reflexId)` after the server side Reflex succeeds and the DOM has been updated
129 - `reflexError(element, reflex, error, reflexId)` whenever the server side Reflex raises an error
130 - `reflexHalted(element, reflex, noop, reflexId)` reflex canceled with throw :abort in the before_reflex callback
131 - `afterReflex(element, reflex, noop, reflexId)` after both success and error
132 - `finalizeReflex(element, reflex, noop, reflexId)` after both success and error
134 ### Client-side callbacks (custom)
136 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:
138 - `beforeAddOne(element, reflex, noop, reflexId)`
139 - `addOneSuccess(element, reflex, noop, reflexId)`
140 - `addOneError(element, reflex, error, reflexId)`
141 - `addOneHalted(element, reflex, noop, reflexId)`
142 - `afterAddOne(element, reflex, noop, reflexId)`
143 - `finalizeAddOne(element, reflex, noop, reflexId)`
145 ### Client-side events
147 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
149 - `stimulus-reflex:before`
150 - `stimulus-reflex:success`
151 - `stimulus-reflex:error`
152 - `stimulus-reflex:halted`
153 - `stimulus-reflex:after`
155 There are also events related to the StimulusReflex library setting up and connecting to ActionCable
157 - `stimulus-reflex:connected`
158 - `stimulus-reflex:disconnected`
159 - `stimulus-reflex:rejected`
160 - `stimulus-reflex:ready`
166 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)
170 `stimulate()` method returns a promise
173 this.stimulate('Comments#create')
174 .then(() => this.doSomething())
175 .catch(() => this.handleError())
178 ### Inheriting data-attributes from parent elements
180 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.
183 <!-- new.html.erb -->
184 <div data-post-id="<%= @post.id %>">
185 <div data-category-id="<%= @category.id %>">
186 <button data-reflex="click->Comment#create" data-reflex-dataset="combined">Create</button>
193 class CommentReflex < ApplicationReflex
195 puts element.dataset["post-id"]
196 puts element.dataset["category-id"]
203 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)
206 <!-- index.html.erb -->
207 <div data-reflex-root="[forward],[backward]">
208 <input type="text" value="<%= @words %>" data-reflex="keyup->Example#words">
209 <div forward><%= @words %></div>
210 <div backward><%= @words&.reverse %></div>
217 @words = element[:value]
221 ### Permanent elements
223 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.
226 <!-- index.html.erb -->
227 <div data-reflex-permanent>
228 <iframe src="https://ghbtns.com/github-btn.html?user=hopsoft&repo=stimulus_reflex&type=star&count=true" frameborder="0" scrolling="0" class="ghbtn"></iframe>
229 <iframe src="https://ghbtns.com/github-btn.html?user=hopsoft&repo=stimulus_reflex&type=fork&count=true" frameborder="0" scrolling="0" class="ghbtn"></iframe>
233 ### Aborting a reflex
235 call `raise :abort` within a reflex method to cancel it.
239 class CommentReflex < ApplicationReflex