Skip to content

Commit ee45c09

Browse files
committed
Check to see if visibility elements should be re-initialized.
1 parent 7fa885b commit ee45c09

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

django_unicorn/static/unicorn/js/messageSender.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,21 @@ export function send(component, callback) {
223223
// Reset all event listeners
224224
component.refreshEventListeners();
225225

226-
// Check for visibility elements
227-
component.initVisibility();
226+
// Check for visibility elements if the last return value from the method wasn't false
227+
let reInitVisbility = true;
228+
229+
component.visibilityEls.forEach((el) => {
230+
if (
231+
el.visibility.method === component.return.method &&
232+
component.return.value === false
233+
) {
234+
reInitVisbility = false;
235+
}
236+
});
237+
238+
if (reInitVisbility) {
239+
component.initVisibility();
240+
}
228241

229242
// Re-add unicorn validation messages from errors
230243
component.modelEls.forEach((element) => {

example/unicorn/components/js.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def select_state(self, val, idx):
3232
self.selected_state = val
3333

3434
def increase_counter(self):
35+
if self.scroll_counter >= 2:
36+
return False
37+
3538
self.scroll_counter += 1
3639

3740
class Meta:

example/unicorn/templates/unicorn/js.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,27 @@
1919
}());
2020
</script>
2121

22-
<h2>
23-
<button unicorn:click="call_javascript">Component view calls JavaScript</button>
24-
<button unicorn:click="call_javascript_module">Component view calls JavaScript module</button>
25-
</h2>
22+
<h3>Visibility</h3>
23+
<div unicorn:key="visibility">
24+
<span unicorn:visible.threshold-25.debounce-1000="increase_counter" unicorn:partial="visibility">
25+
Number of times this span was scrolled into view: {{ scroll_counter }}
26+
</span>
27+
</div>
28+
29+
<h3>Call JavaScript</h3>
30+
<button unicorn:click="call_javascript">Component view calls JavaScript</button>
31+
<button unicorn:click="call_javascript_module">Component view calls JavaScript module</button>
2632

2733
<div>
28-
<h2>
34+
<h3>
2935
<code>javascript_exclude states</code>
30-
</h2>
36+
</h3>
3137
{% for state in states %}
3238
{{ state }}
3339
{% endfor %}
3440
</div>
3541

36-
<h2>Select2</h2>
42+
<h3>Select2</h3>
3743

3844
<div u:ignore>
3945
<select unicorn:model="selected_state" class="form-control" id="select2-example" required
@@ -66,10 +72,4 @@ <h2>Select2</h2>
6672
select2_datetime: {{ select2_datetime }}
6773
</div>
6874
</div>
69-
70-
<div unicorn:key="visibility">
71-
<span unicorn:visible.threshold-25.debounce-1000="increase_counter" unicorn:partial="visibility">
72-
Number of times this span was scrolled into view: {{ scroll_counter }}
73-
</span>
74-
</div>
7575
</div>

0 commit comments

Comments
 (0)