@mschop/alpine-web-components 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AlpineWebComponent.ts +11 -10
- package/package.json +1 -1
package/AlpineWebComponent.ts
CHANGED
|
@@ -24,8 +24,7 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
24
24
|
protected abstract template: string
|
|
25
25
|
protected styles: () => string = () => ``
|
|
26
26
|
protected attributeCasts: AttributeCasts = {};
|
|
27
|
-
protected
|
|
28
|
-
private alpineInitHandler = () => this.init()
|
|
27
|
+
protected alpineInitHandler = () => this.init()
|
|
29
28
|
|
|
30
29
|
constructor() {
|
|
31
30
|
super()
|
|
@@ -62,7 +61,9 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
62
61
|
this.state.attributes = {};
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
|
|
64
|
+
const castedNewValue = this.castFromAttribute(name, newValue);
|
|
65
|
+
|
|
66
|
+
if (oldValue === castedNewValue) {
|
|
66
67
|
return;
|
|
67
68
|
}
|
|
68
69
|
|
|
@@ -70,14 +71,14 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
70
71
|
return;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
this.state.attributes[name] =
|
|
74
|
+
this.state.attributes[name] = castedNewValue;
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
protected loadTemplate() {
|
|
77
78
|
this.getRoot().innerHTML = this.template;
|
|
78
79
|
}
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
protected bootState() {
|
|
81
82
|
let state = this.state;
|
|
82
83
|
|
|
83
84
|
// @ts-ignore
|
|
@@ -92,14 +93,14 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
92
93
|
this.state = Alpine.reactive(state);
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
|
|
96
|
+
protected bootAlpine() {
|
|
96
97
|
// @ts-ignore
|
|
97
98
|
Alpine.addScopeToNode(this.getRoot(), this.state)
|
|
98
99
|
// @ts-ignore
|
|
99
100
|
Alpine.initTree(this.getRoot())
|
|
100
101
|
}
|
|
101
102
|
|
|
102
|
-
|
|
103
|
+
protected bootAttributes(): void {
|
|
103
104
|
// @ts-ignore
|
|
104
105
|
this.constructor.observedAttributes?.forEach((key: string) => {
|
|
105
106
|
const value = this.getAttribute(key);
|
|
@@ -118,11 +119,11 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
118
119
|
})
|
|
119
120
|
}
|
|
120
121
|
|
|
121
|
-
|
|
122
|
+
protected castToAttribute(name: string, value: any): null|string {
|
|
122
123
|
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].toAttribute(value);
|
|
123
124
|
}
|
|
124
125
|
|
|
125
|
-
|
|
126
|
+
protected castFromAttribute(name: string, value: null|string): any {
|
|
126
127
|
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].fromAttribute(value);
|
|
127
128
|
}
|
|
128
129
|
|