Skip to content

Commit 65a108b

Browse files
committed
Support targeting multiple partials.
1 parent 5b1ecae commit 65a108b

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

django_unicorn/static/js/component.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ export class Component {
249249
/**
250250
* Calls the method for a particular component.
251251
*/
252-
callMethod(methodName, partial, errCallback) {
252+
callMethod(methodName, partials, errCallback) {
253253
const action = {
254254
type: "callMethod",
255255
payload: { name: methodName },
256-
partial,
256+
partials,
257257
};
258258
this.actionQueue.push(action);
259259

@@ -334,13 +334,13 @@ export class Component {
334334
false
335335
);
336336

337-
this.poll.partial = rootElement.partial;
337+
this.poll.partials = rootElement.partials;
338338

339339
if (this.isPollEnabled()) {
340340
// Call the method once before the timer starts
341341
this.callMethod(
342342
this.poll.method,
343-
this.poll.partial,
343+
this.poll.partials,
344344
this.handlePollError
345345
);
346346
}
@@ -357,7 +357,7 @@ export class Component {
357357
if (this.isPollEnabled()) {
358358
this.callMethod(
359359
this.poll.method,
360-
this.poll.partial,
360+
this.poll.partials,
361361
this.handlePollError
362362
);
363363
}

django_unicorn/static/js/element.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class Element {
3131
this.actions = [];
3232
this.db = {};
3333
this.field = {};
34+
this.partials = [];
3435
this.target = null;
35-
this.partial = {};
3636
this.key = null;
3737
this.events = [];
3838
this.errors = [];
@@ -100,11 +100,11 @@ export class Element {
100100
this.target = attribute.value;
101101
} else if (attribute.isPartial) {
102102
if (attribute.modifiers.id) {
103-
this.partial.id = attribute.value;
103+
this.partials.push({ id: attribute.value });
104104
} else if (attribute.modifiers.key) {
105-
this.partial.key = attribute.value;
105+
this.partials.push({ key: attribute.value });
106106
} else {
107-
this.partial.target = attribute.value;
107+
this.partials.push({ target: attribute.value });
108108
}
109109
} else if (attribute.eventType) {
110110
const action = {};

django_unicorn/static/js/eventListeners.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ export function addActionEventListener(component, eventType) {
239239
if (action.key) {
240240
if (action.key === toKebabCase(event.key)) {
241241
handleLoading(component, targetElement);
242-
component.callMethod(action.name, targetElement.partial);
242+
component.callMethod(action.name, targetElement.partials);
243243
}
244244
} else {
245245
handleLoading(component, targetElement);
246-
component.callMethod(action.name, targetElement.partial);
246+
component.callMethod(action.name, targetElement.partials);
247247
}
248248
}
249249
});
@@ -291,7 +291,7 @@ export function addModelEventListener(component, element, eventType) {
291291
name: element.model.name,
292292
value: element.getValue(),
293293
},
294-
partial: element.partial,
294+
partials: element.partials,
295295
};
296296

297297
if (!component.lastTriggeringElements.some((e) => e.isSame(element))) {
@@ -408,7 +408,7 @@ export function addDbEventListener(component, element, eventType) {
408408
db: element.db,
409409
fields: {},
410410
},
411-
partial: element.partial,
411+
partials: element.partials,
412412
};
413413

414414
action.payload.fields[element.field.name] = element.getValue();

django_unicorn/views/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def _process_component_request(
101101
for action in component_request.action_queue:
102102
if action.partial:
103103
partials.append(action.partial)
104+
else:
105+
partials = action.partials
104106

105107
if action.action_type == "syncInput":
106108
sync_input.handle(component_request, component, action.payload)

django_unicorn/views/objects.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class Action:
1919
def __init__(self, data):
2020
self.action_type = data.get("type")
2121
self.payload = data.get("payload", {})
22-
self.partial = data.get("partial")
22+
self.partial = data.get("partial") # this is deprecated, but leaving it for now
23+
self.partials = data.get("partials", [])
2324

2425
def __repr__(self):
25-
return f"Action(action_type='{self.action_type}' payload={self.payload} partial={self.partial})"
26+
return f"Action(action_type='{self.action_type}' payload={self.payload} partials={self.partials})"
2627

2728

2829
class ComponentRequest:

example/unicorn/templates/unicorn/html-inputs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ <h2>Boolean Toggles</h2>
1212
<button unicorn:click="$toggle('is_checked', 'another_check')">Toggle booleans (normal)</button><br />
1313
<button unicorn:click="$toggle('is_checked', 'another_check')" u:partial.key="boolean-toggles-key">Toggle booleans (partial.key)</button>
1414
<button unicorn:click="$toggle('is_checked', 'another_check')" u:partial.id="boolean-toggles-id">Toggle booleans (partial.id)</button><br />
15+
<button unicorn:click="$toggle('is_checked', 'another_check')" u:partial.id="boolean-toggles-id" u:partial.key="boolean-toggles-key">Toggle booleans (multiple partials)</button><br />
1516
<button unicorn:click="$toggle('is_checked', 'another_check')" u:partial="boolean-toggles-id">Toggle booleans (partial boolean-toggles-id)</button>
1617
<button unicorn:click="$toggle('is_checked', 'another_check')" u:partial="boolean-toggles-key">Toggle booleans (partial boolean-toggles-key)</button>
1718
</div>

tests/js/element/partial.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@ import { getElement } from "../utils.js";
33

44
test("partial target", (t) => {
55
const html =
6-
"<button unicorn:click='test_click' unicorn:partial='test-id'>Click</button>";
6+
"<button unicorn:click='test_click' unicorn:partial='test-target'>Click</button>";
77
const element = getElement(html);
88

9-
t.is(element.partial.target, "test-id");
9+
t.is(element.partials[0].target, "test-target");
1010
});
1111

1212
test("partial.id", (t) => {
1313
const html =
1414
"<button unicorn:click='test_click' unicorn:partial.id='test-id'>Click</button>";
1515
const element = getElement(html);
1616

17-
t.is(element.partial.id, "test-id");
17+
t.is(element.partials[0].id, "test-id");
1818
});
1919

2020
test("partial.key", (t) => {
2121
const html =
22-
"<button unicorn:click='test_click' unicorn:partial.key='test-id'>Click</button>";
22+
"<button unicorn:click='test_click' unicorn:partial.key='test-key'>Click</button>";
2323
const element = getElement(html);
2424

25-
t.is(element.partial.key, "test-id");
25+
t.is(element.partials[0].key, "test-key");
26+
});
27+
28+
test("multiple partials", (t) => {
29+
const html =
30+
"<button unicorn:click='test_click' unicorn:partial.id='test-id' unicorn:partial.key='test-key'>Click</button>";
31+
const element = getElement(html);
32+
33+
t.is(element.partials[0].id, "test-id");
34+
t.is(element.partials[1].key, "test-key");
2635
});

0 commit comments

Comments
 (0)