You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reflexes.md
+57-18Lines changed: 57 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ description: "Reflex classes are full of Reflex actions. Reflex actions? Full of
4
4
5
5
# Reflexes
6
6
7
-
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 Rails controller or Django view and action that originally rendered the page.
7
+
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.
8
8
9
9
## Glossary
10
10
@@ -26,11 +26,11 @@ All Stimulus controllers that have had `StimulusReflex.register(this)` called in
**target**, required \(exception: see "Requesting a Refresh" below\): A string containing the server Reflex class and method, in the form "ExampleReflex\#increment".
29
+
-**target**, required \(exception: see "Requesting a Refresh" below\): A string containing the server Reflex class and method, in the form "ExampleReflex\#increment".
30
30
31
-
**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**.
31
+
-**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**.
32
32
33
-
**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.
33
+
-**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.
34
34
35
35
### Requesting a "refresh"
36
36
@@ -48,31 +48,70 @@ It's also possible to trigger this global Reflex by passing nothing but a browse
48
48
<button data-reflex="click">Refresh</button>
49
49
```
50
50
51
-
## Reflex Classes
51
+
## The Reflex Class
52
52
53
53
StimulusReflex makes the following properties available to the developer inside Reflex actions:
54
54
55
-
{% tabs %}
56
-
{% tab title="Python" %}
57
55
## Properties
58
-
*`consumer` - the Websocket connection from django channels.
59
-
*`request` - a django request object
60
-
*`request.post` - If the page contains a form, this will find the closest form.
61
-
*`session` - the Django session store for the current visitor
62
-
*`url` - the URL of the page that triggered the reflex
63
-
*`element` - an object that represents the HTML element that triggered the reflex
64
-
*`params` - Contains the form parameters for the closest form
56
+
-`consumer` - the Websocket connection from django channels.
57
+
-`request` - a django request object
58
+
-`request.post` - If the page contains a form, it will find the closest form which and the parameters will be contained here.
59
+
-`session` - the Django session store for the current visitor
60
+
-`url` - the URL of the page that triggered the reflex
61
+
-`element` - an object that represents the HTML element that triggered the reflex
62
+
-`params` - Contains the form parameters for the closest form
65
63
66
64
## Methods
67
-
*`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 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.
68
-
{% endtab %}
69
-
{% endtabs %}
65
+
-`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 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.
66
+
67
+
-`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.
70
68
71
69
{% hint style="danger" %}
72
70
`reflex` and `process` are reserved words inside Reflex classes. You cannot create Reflex actions with these names.
73
71
{% endhint %}
74
72
75
-
### `element`
73
+
### Modify or add to the view context
74
+
75
+
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.
76
+
77
+
{% tabs %}
78
+
{% tab %}
79
+
```python
80
+
from sockpuppet.reflex import Reflex
81
+
82
+
classExampleReflex(Reflex):
83
+
defwork(self):
84
+
# All new instance variables in the reflex will be accessible
85
+
# in the context during rendering.
86
+
self.instance_variable ='hello world'
87
+
88
+
context =self.get_context_data()
89
+
context['a_key'] ='a pink elephant'
90
+
# If "a_key" existed in the context before the reflex was triggered
91
+
# the context variable will now be modified to "a pink elephant"
92
+
93
+
# if it didn't exist, the context variable is then created with the
94
+
# data "a pink elephant" 🐘
95
+
96
+
```
97
+
{% endtab %}
98
+
99
+
{% tab %}
100
+
```html
101
+
<div>
102
+
<!-- When the work reflex is triggered "new_variable" will be
103
+
available in the context -->
104
+
<span>{{ instance_variable }}</span>
105
+
106
+
<!-- This will show up as "a pink elephant" when triggering the reflex -->
107
+
<span>{{ a_key }}</span>
108
+
</div>
109
+
110
+
```
111
+
{% endtab %}
112
+
{% endtabs %}
113
+
114
+
### The `element` property
76
115
77
116
The `element` property contains all of the Reflex controller's [DOM element attributes](https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes) as well as other properties like, `tag_name`, `checked` and `value`.
0 commit comments