Skip to content

Commit 03a0d99

Browse files
committed
Prevent an action for lazy and defer models/dbModels when there isn't a change to the element value.
1 parent 1942f18 commit 03a0d99

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

django_unicorn/static/js/eventListeners.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,26 @@ export function addModelEventListener(component, element, eventType) {
262262
const { el } = element;
263263

264264
el.addEventListener(eventType, (event) => {
265+
let isDirty = false;
266+
265267
if (component.data[element.model.name] !== element.getValue()) {
268+
isDirty = true;
266269
element.handleDirty();
267270
} else {
268271
element.handleDirty(true);
269272
}
270273

271-
// Lazy models fire an input and blur so that the dirty check above works as expected.
272-
// This will prevent the input event from doing anything.
273-
if (element.model.isLazy && eventType === "input") {
274-
return;
274+
if (element.model.isLazy) {
275+
// Lazy models fire an input and blur so that the dirty check above works as expected.
276+
// This will prevent the input event from doing anything.
277+
if (eventType === "input") {
278+
return;
279+
}
280+
281+
// Lazy non-dirty elements can bail
282+
if (!isDirty) {
283+
return;
284+
}
275285
}
276286

277287
const action = {
@@ -287,21 +297,26 @@ export function addModelEventListener(component, element, eventType) {
287297
}
288298

289299
if (element.model.isDefer) {
290-
let foundAction = false;
300+
let foundActionIdx = -1;
291301

292302
// Update the existing action with the current value
293-
component.actionQueue.forEach((a) => {
303+
component.actionQueue.forEach((a, idx) => {
294304
if (a.payload.name === element.model.name) {
295305
a.payload.value = element.getValue();
296-
foundAction = true;
306+
foundActionIdx = idx;
297307
}
298308
});
299309

300310
// Add a new action
301-
if (!foundAction) {
311+
if (isDirty && foundActionIdx === -1) {
302312
component.actionQueue.push(action);
303313
}
304314

315+
// Remove the found action that isn't dirty
316+
if (!isDirty && foundActionIdx > -1) {
317+
component.actionQueue.splice(foundActionIdx);
318+
}
319+
305320
return;
306321
}
307322

@@ -348,22 +363,31 @@ export function addDbEventListener(component, element, eventType) {
348363
return;
349364
}
350365

366+
let isDirty = false;
367+
351368
for (let i = 0; i < component.data[element.model.name].length; i++) {
352369
const dbModel = component.data[element.model.name][i];
353370

354371
if (dbModel.pk.toString() === element.db.pk) {
355372
if (dbModel[element.field.name] !== element.getValue()) {
356373
element.handleDirty();
374+
isDirty = true;
357375
} else {
358376
element.handleDirty(true);
359377
}
360378
}
361379
}
362380

363-
// Lazy models fire an input and blur so that the dirty check above works as expected.
364-
// This will prevent the input event from doing anything.
365-
if (element.field.isLazy && eventType === "input") {
366-
return;
381+
if (element.field.isLazy) {
382+
// Lazy models fire an input and blur so that the dirty check above works as expected.
383+
// This will prevent the input event from doing anything.
384+
if (eventType === "input") {
385+
return;
386+
}
387+
// Lazy non-dirty elements can bail
388+
if (!isDirty) {
389+
return;
390+
}
367391
}
368392

369393
if (!component.lastTriggeringElements.some((e) => e.isSame(element))) {
@@ -382,21 +406,26 @@ export function addDbEventListener(component, element, eventType) {
382406
action.payload.fields[element.field.name] = element.getValue();
383407

384408
if (element.field.isDefer) {
385-
let foundAction = false;
409+
let foundActionIdx = -1;
386410

387411
// Update the existing action with the current value
388-
component.actionQueue.forEach((a) => {
412+
component.actionQueue.forEach((a, idx) => {
389413
if (generateDbKey(a.payload) === element.dbKey()) {
390414
a.payload.fields[element.field.name] = element.getValue();
391-
foundAction = true;
415+
foundActionIdx = idx;
392416
}
393417
});
394418

395419
// Add a new action
396-
if (!foundAction) {
420+
if (isDirty && foundActionIdx === -1) {
397421
component.actionQueue.push(action);
398422
}
399423

424+
// Remove the found action that isn't dirty
425+
if (!isDirty && foundActionIdx > -1) {
426+
component.actionQueue.splice(foundActionIdx);
427+
}
428+
400429
return;
401430
}
402431

0 commit comments

Comments
 (0)