-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi-reactive.js
More file actions
executable file
·47 lines (39 loc) · 1.33 KB
/
i-reactive.js
File metadata and controls
executable file
·47 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class iReactive {
constructor({ data, mounted }) {
const dataToProxy = data();
this.allDynamicElements = document.querySelectorAll("*[i-data]");
this.allDynamicBinds = document.querySelectorAll("*[i-bind]");
this.allDynamicInput = document.querySelectorAll("*[i-model]");
this.$data = new Proxy(dataToProxy, {
get: (target, name) => {
return name in target ? target[name] : "";
},
set: (target, name, val) => {
target[name] = val;
this._render();
}
});
this.allDynamicInput.forEach(el => {
el.addEventListener("input", () => {
Reflect.set(this.$data, el.getAttribute("i-model"), el.value);
});
});
this._render();
Reflect.apply(mounted, this.$data, []);
}
_render() {
this.allDynamicElements.forEach(el => {
const content = Reflect.get(this.$data, el.getAttribute("i-data"));
el.innerText = content;
});
this.allDynamicBinds.forEach(el => {
const [attr, target] = el.getAttribute("i-bind").match(/(\w+)/g);
const content = Reflect.get(this.$data, target);
el.setAttribute(attr, content);
});
this.allDynamicInput.forEach(el => {
const content = Reflect.get(this.$data, el.getAttribute("i-model"));
el.value = content;
});
}
}