Reflexes
Reflex classes are full of Reflex actions. Reflex actions? Full of love. 🏩
Server-side Reflexes inherit from sockpuppet.Reflex
. They hold logic responsible for performing operations like writing to your backend data stores. Reflexes are not concerned with rendering because rendering is delegated to the Django view.
Glossary
Sockpuppet: The name of this project, which has a JS websocket client and a Django-based server component, which is based on
django-channels
.Stimulus: An incredibly simple yet powerful JS framework by the creators of Rails.
"a Reflex": Used to describe the full, round-trip life-cycle of a Sockpuppet operation, from client to server and back again
Reflex class: A Python class that inherits from
sockpuppet.Reflex
and lives in yourreflexes
folder orreflex.py
, this is where your Reflex actions are implemented.Reflex action: A method in a Reflex class, called in response to activity in the browser. It has access to several special accessors containing all of the Reflex controller element's attributes
Reflex controller: A Stimulus controller that imports the StimulusReflex client library. It has a
stimulate
method for triggering Reflexes and like all Stimulus controllers, it's aware of the element it is attached to - as well as any Stimulus targets in its DOM hierarchyReflex controller element: The DOM element upon which the
data-reflex
attribute is placed, which often has data attributes intended to be delivered to the server during a Reflex action
Calling a Reflex
Regardless of whether you use declarative Reflex calls via data-reflex
attributes in your HTML or if you are using JavaScript, ultimately the stimulate
method on your Stimulus controller is being called. We touched on this briefly in the Quick Start chapter; now we are going to document the function signature so that you fully understand what's happening behind the scenes.
All Stimulus controllers that have had StimulusReflex.register(this)
called in their connect
method gain a stimulate
method.
target, required (exception: see "Requesting a Refresh" below): A string containing the server Reflex class and method, in the form "ExampleReflex#increment".
element, optional: A reference to a DOM element which will provide both attributes and scoping selectors. Frequently pointed to
event.target
in JavaScript. Defaults to the DOM element of the controller in scope.argument, optional: A splat of JSON-compliant JavaScript datatypes - array, object, string, numeric or boolean - can be received by the server Reflex action as one or many ordered arguments. Defaults to no argument(s). Note: the method signature has to match. If the Reflex action is expecting two arguments and doesn't receive two arguments, it will raise an exception.
Requesting a "refresh"
If you are building advanced workflows, there are edge cases where you may want to initiate a Reflex action that does nothing but re-render the view template and morph any new changes into the DOM. While this shouldn't be your primary tool, it's possible for your data to be mutated by destructive external side effects. 🧟
Calling stimulate
with no parameters invokes a special global Reflex that allows you to force a re-render of the current state of your application UI. This is the same thing that the user would see if they hit their browser's Refresh button, except without the painfully slow round-trip cycle.
It's also possible to trigger this global Reflex by passing nothing but a browser event to the data-reflex
attribute. For example, the following button element will refresh the page content every time the user presses it:
The Reflex Class
StimulusReflex makes the following properties available to the developer inside Reflex actions:
Properties
consumer
- the Websocket connection from django channels.request
- a django request objectrequest.post
- If the page contains a form, it will find the closest form which and the parameters will be contained here.session
- the Django session store for the current visitorurl
- the URL of the page that triggered the reflexelement
- an object that represents the HTML element that triggered the reflexparams
- Contains the form parameters for the closest form
Methods
get_context_data
- Accesses the context data from the view associated with the reflex. You will know that the method is triggered from the reflex because the context now containsstimulus_reflex
which is equal toTrue
. This will be available fromkwargs
so you can modify the context based on whether it is a reflex or not.get_channel_id
- By default this returns the session key which is used to deliver the websocket update to the client. This function can be overridden if you need a different key for transferring the update.
reflex
and process
are reserved words inside Reflex classes. You cannot create Reflex actions with these names.
Modify or add to the view context
When a reflex is triggered you can modify the current context of the view or add more context which previously didn't exist when the view rendered in the normal request-response cycle.
The element
property
element
propertyThe element
property contains all of the Reflex controller's DOM element attributes as well as other properties like, tag_name
, checked
and value
.
Most values are strings. The only exceptions are checked
and selected
which are booleans.
Elements that support multiple values (like <select multiple>
, or a collection of checkboxes with equal name
), will emit an additional values
property. The value
property will contain a comma-separated string of the checked options.
Here's an example that outlines how you can interact with the element
property in your Reflexes.
When Sockpuppet is rendering your template, a context variable named stimulus_reflex is available to your Django view and set to true.
You can use this flag to create branching logic to control how the template might look different if it's a Reflex versus a normal page refresh.
Inheriting data-attributes from parent elements
You might design your interface such that you have a deeply nested structure of data attributes on parent elements. Instead of writing code to travel your DOM and access those values, you can use the data-reflex-dataset="combined"
directive to scoop all data attributes up the hierarchy and pass them as part of the Reflex payload.
This Reflex action will have post-id
and category-id
accessible:
If a data attribute appears several times, the deepest one in the DOM tree is taken. In the following example, data-id
would be 2.
Last updated