-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini-vue.js
91 lines (73 loc) · 2.44 KB
/
mini-vue.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class PlatziReactive {
//Dependencias
deps = new Map();
constructor(options) {
this.data = options.data();
const self = this;
this.$data = new Proxy(this.data, {
get(target, name) {
if(Reflect.has(target, name)) {
self.track(target, name);
return Reflect.get(target, name);
}
console.warn("La propiedad no existe: ", name);
return "";
},
set(target, name, value) {
Reflect.set(target, name, value);
self.trigger(name)
}
});
}
track(target, name){
if(!this.deps.has(name)) {
const effect = () => {
document.querySelectorAll(`*[p-text=${name}]`).forEach(el => {
this.pText(el, target, name)
});
document.querySelectorAll(`*[p-model=${name}]`).forEach(el => {
this.pModel(el, target, name)
});
document.querySelectorAll(`*[p-bind=${name}]`).forEach(el => {
const [attr, name] = el.getAttribute("p-bind").match(/(\w+)/g);
this.pBind(el, target, name, attr);
});
}
this.deps.set(name, effect);
}
}
trigger(name){
const effect = this.deps.get(name);
effect();
}
mount() {
document.querySelectorAll("*[p-text]").forEach(el => {
this.pText(el, this.$data, el.getAttribute("p-text"))
});
document.querySelectorAll("*[p-model]").forEach(el => {
const name = el.getAttribute("p-model");
this.pModel(el, this.$data, name);
el.addEventListener('input', () => {
Reflect.set(this.$data, name, el.value);
});
});
document.querySelectorAll("*[p-bind]").forEach(el => {
const [attr, name] = el.getAttribute("p-bind").match(/(\w+)/g);
this.pBind(el, this.$data, name, attr);
})
}
pText(el, data, name) {
el.innerText = Reflect.get(data, name);
}
pModel(el, data, name) {
el.value = Reflect.get(data, name);
}
pBind(el, data, name, attr) {
el.setAttribute(attr, Reflect.get(data, name));
}
}
var miniVue = {
createApp(options) {
return new PlatziReactive(options)
}
}