Skip to content

Commit 8da5a0c

Browse files
committed
Add hasValue to utils.
1 parent 804ea9f commit 8da5a0c

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

django_unicorn/static/js/element.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Attribute } from "./attribute.js";
2-
import { isEmpty } from "./utils.js";
2+
import { isEmpty, hasValue } from "./utils.js";
33

44
/**
55
* Encapsulate DOM element for Unicorn-related information.
@@ -166,11 +166,7 @@ export class Element {
166166
* A key that takes into consideration the db name and pk.
167167
*/
168168
dbKey() {
169-
if (
170-
!isEmpty(this.db) &&
171-
typeof this.db.pk !== "undefined" &&
172-
typeof this.db.name !== "undefined"
173-
) {
169+
if (hasValue(this.db) && hasValue(this.db.pk) && hasValue(this.db.name)) {
174170
return `${this.db.name}:${this.db.pk}`;
175171
}
176172

@@ -203,7 +199,7 @@ export class Element {
203199
* Sets the value of an element. Tries to deal with HTML weirdnesses.
204200
*/
205201
setValue(val) {
206-
if (typeof this.el.type === "undefined") {
202+
if (isEmpty(this.el.type)) {
207203
return;
208204
}
209205

django_unicorn/static/js/unicorn.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component } from "./component.js";
2+
import { hasValue } from "./utils.js";
23

34
let messageUrl = "";
45
const csrfTokenHeaderName = "X-CSRFToken";
@@ -33,7 +34,7 @@ export function call(componentName, methodName) {
3334
let component;
3435

3536
Object.keys(components).forEach((id) => {
36-
if (typeof component === "undefined") {
37+
if (hasValue(component)) {
3738
const _component = components[id];
3839

3940
if (_component.name === componentName) {

django_unicorn/static/js/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Checks if an object is empty. Useful for check if a dictionary has any values.
2+
* Checks if an object is empty. Useful to check if a dictionary has a value.
33
*/
44
export function isEmpty(obj) {
55
return (
@@ -9,6 +9,13 @@ export function isEmpty(obj) {
99
);
1010
}
1111

12+
/**
13+
* Checks if an object has a value.
14+
*/
15+
export function hasValue(obj) {
16+
return !isEmpty(obj);
17+
}
18+
1219
/**
1320
* Checks if a string has the search text.
1421
*/

0 commit comments

Comments
 (0)