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.django-channels
.sockpuppet.Reflex
and lives in your reflexes
folder or reflex.py
, this is where your Reflex actions are implemented.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 hierarchydata-reflex
attribute is placed, which often has data attributes intended to be delivered to the server during a Reflex actiondata-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.StimulusReflex.register(this)
called in their connect
method gain a stimulate
method.event.target
in JavaScript. Defaults to the DOM element of the controller in scope.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.data-reflex
attribute. For example, the following button element will refresh the page content every time the user presses it: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 formget_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 contains stimulus_reflex
which is equal to True
. This will be available from kwargs
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.element
propertyelement
property contains all of the Reflex controller's DOM element attributes as well as other properties like, tag_name
, checked
and value
.checked
and selected
which are booleans.<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.element
property in your Reflexes.data-reflex-dataset="combined"
directive to scoop all data attributes up the hierarchy and pass them as part of the Reflex payload.post-id
and category-id
accessible:data-id
would be 2.