Skip to content

Commit 3fdf1f0

Browse files
Document how to call a method on another component
1 parent f80b27e commit 3fdf1f0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/source/javascript.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,48 @@ class CallJavascriptView(UnicornView):
3030

3131
def hello(self):
3232
self.call("hello", self.name)
33+
34+
## Call Method on Other Component
35+
36+
From a component, it is possible to call a method on another component. This is useful when you want to trigger a refresh or an update on another component.
37+
38+
```html
39+
<!-- compare-list.html -->
40+
<div>
41+
<!-- content -->
42+
</div>
43+
```
44+
45+
```python
46+
# compare_list.py
47+
from django_unicorn.components import UnicornView
48+
49+
class CompareListView(UnicornView):
50+
def reload_state(self):
51+
# do something
52+
pass
53+
```
54+
55+
```html
56+
<!-- source-checkbox.html -->
57+
<div>
58+
<input type="checkbox" unicorn:model="is_checked" unicorn:change="toggle_check_state" />
59+
</div>
60+
```
61+
62+
```python
63+
# source_checkbox.py
64+
from django_unicorn.components import UnicornView
65+
66+
class SourceCheckboxView(UnicornView):
67+
is_checked = False
68+
69+
def toggle_check_state(self):
70+
self.is_checked = not self.is_checked
71+
self.call("Unicorn.call", "compare-list", "reload_state")
72+
```
73+
74+
The first argument to `call` is the name of the JavaScript function to call, which is `Unicorn.call`. The second argument is the name (or key) of the component to call. The third argument is the name of the method to call on that component.
3375
```
3476
3577
## Trigger Model Update

0 commit comments

Comments
 (0)