Skip to content

Commit 76144ed

Browse files
committed
Create a clearer description of modifying context in a reflex
1 parent 23f5bf4 commit 76144ed

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

.github/workflows/changelog.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ on:
55
release:
66
types: [created]
77
push:
8-
branches:
9-
- master
8+
tags:
9+
- '*'
1010

1111
jobs:
1212
build:

docs/patterns.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ For example, if your controller is named _list-item_ you might consider **this.e
176176

177177
## Server-Side
178178

179-
### Rendering views inside of an ActiveRecord model or ActiveJob class
179+
### Rendering views inside of a Django model
180180

181181
If you plan to broadcast an update of an html template from somewhere outside a reflex you can draw from the example below.
182182

@@ -218,10 +218,10 @@ class NotificationReflex(Reflex):
218218

219219
def force_update(id)
220220
channel = Channel(self.consumer.scope['session'].session_key)
221-
channel.dispatch_event {
222-
name: "force:update",
223-
detail: {id: id},
224-
}
221+
channel.dispatch_event({
222+
name: "force:update",
223+
detail: {id: id},
224+
})
225225
channel.broadcast()
226226

227227
def reload(self):
@@ -233,7 +233,7 @@ class NotificationReflex(Reflex):
233233

234234
{% tabs %}
235235
{% tab title="index.html" %}
236-
```markup
236+
```html
237237
<div data-action="force:update@document->notification#reload">
238238
<button data-action="notification#forceUpdate">
239239
</div>

docs/reflexes.md

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Reflex classes are full of Reflex actions. Reflex actions? Full of
44

55
# Reflexes
66

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.
88

99
## Glossary
1010

@@ -26,11 +26,11 @@ All Stimulus controllers that have had `StimulusReflex.register(this)` called in
2626
this.stimulate(string target, [DOMElement element], ...[JSONObject argument])
2727
```
2828

29-
**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".
3030

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**.
3232

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.
3434

3535
### Requesting a "refresh"
3636

@@ -48,31 +48,70 @@ It's also possible to trigger this global Reflex by passing nothing but a browse
4848
<button data-reflex="click">Refresh</button>
4949
```
5050

51-
## Reflex Classes
51+
## The Reflex Class
5252

5353
StimulusReflex makes the following properties available to the developer inside Reflex actions:
5454

55-
{% tabs %}
56-
{% tab title="Python" %}
5755
## 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
6563

6664
## 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.
7068

7169
{% hint style="danger" %}
7270
`reflex` and `process` are reserved words inside Reflex classes. You cannot create Reflex actions with these names.
7371
{% endhint %}
7472

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+
class ExampleReflex(Reflex):
83+
def work(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
76115

77116
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`.
78117

0 commit comments

Comments
 (0)