Skip to content

Commit 5d8143b

Browse files
committed
Fix issues where unicorn:db was used without unicorn:model.
1 parent bc53e06 commit 5d8143b

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

django_unicorn/static/js/component.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ export class Component {
177177
const element = new Element(el);
178178

179179
if (element.isUnicorn) {
180-
if (
181-
hasValue(element.field) &&
182-
(hasValue(element.db) || hasValue(element.model))
183-
) {
180+
if (hasValue(element.field) && hasValue(element.db)) {
184181
if (!this.attachedDbEvents.some((e) => e.isSame(element))) {
185182
this.attachedDbEvents.push(element);
186183
addDbEventListener(this, element);
@@ -227,6 +224,8 @@ export class Component {
227224
this.keyEls.push(element);
228225
}
229226

227+
console.log(element.actions);
228+
230229
element.actions.forEach((action) => {
231230
if (this.actionEvents[action.eventType]) {
232231
this.actionEvents[action.eventType].push({ action, element });
@@ -421,15 +420,19 @@ export class Component {
421420
*/
422421
setDbModelValues() {
423422
this.dbEls.forEach((element) => {
424-
if (element.db.pk === "") {
423+
if (isEmpty(element.db.pk)) {
425424
// Empty string for the PK implies that the model is not associated to an actual model instance
426425
element.setValue("");
427426
} else {
428-
if (isEmpty(element.model.name)) {
429-
throw Error("Setting a field value requires a model to be set");
427+
const dbName = element.db.name || element.model.name;
428+
429+
if (isEmpty(dbName)) {
430+
throw Error(
431+
"Setting a field value requires a db or model name to be set"
432+
);
430433
}
431434

432-
let datas = this.data[element.model.name];
435+
let datas = this.data[dbName];
433436

434437
// Force the data to be an array if it isn't already for the next step
435438
if (!Array.isArray(datas)) {
@@ -438,7 +441,7 @@ export class Component {
438441

439442
datas.forEach((model) => {
440443
// Convert the model's pk to a string because it will always be a string on the element
441-
if (hasValue(model.pk)) {
444+
if (hasValue(model) && hasValue(model.pk)) {
442445
if (model.pk.toString() === element.db.pk) {
443446
element.setValue(model[element.field.name]);
444447
}

django_unicorn/static/js/eventListeners.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,26 +358,31 @@ export function addDbEventListener(component, element, eventType) {
358358
const { el } = element;
359359

360360
el.addEventListener(eventType, (event) => {
361-
if (
362-
(isEmpty(element.db.name) && isEmpty(element.model.name)) ||
363-
isEmpty(element.db.pk)
364-
) {
361+
if (isEmpty(element.db.name) && isEmpty(element.model.name)) {
365362
return;
366363
}
367364

368365
let isDirty = false;
369366

370-
for (let i = 0; i < component.data[element.model.name].length; i++) {
371-
const dbModel = component.data[element.model.name][i];
367+
if (
368+
hasValue(element.model.name) &&
369+
Object.prototype.hasOwnProperty.call(component.data, element.model.name)
370+
) {
371+
for (let i = 0; i < component.data[element.model.name].length; i++) {
372+
const dbModel = component.data[element.model.name][i];
372373

373-
if (dbModel.pk.toString() === element.db.pk) {
374-
if (dbModel[element.field.name] !== element.getValue()) {
375-
element.handleDirty();
376-
isDirty = true;
377-
} else {
378-
element.handleDirty(true);
374+
if (dbModel.pk.toString() === element.db.pk) {
375+
if (dbModel[element.field.name] !== element.getValue()) {
376+
element.handleDirty();
377+
isDirty = true;
378+
} else {
379+
element.handleDirty(true);
380+
}
379381
}
380382
}
383+
} else {
384+
// If the data can't be found consider it always dirty since it's new
385+
isDirty = true;
381386
}
382387

383388
if (element.field.isLazy) {

0 commit comments

Comments
 (0)