-
Notifications
You must be signed in to change notification settings - Fork 88
/
minivue.html
112 lines (102 loc) · 3.79 KB
/
minivue.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Minu Vue</title>
</head>
<body>
<script>
let createApp = function (options) {
let dom = null
let keys = Object.keys(options.data())
console.log('keys', keys)
let callbacks = {};
keys.forEach(key => callbacks[key] = [])
console.log(callbacks)
function reactive(obj) {
return new Proxy(obj, {
get(target, key) {
console.log('reactive get', key)
return target[key]
},
set(target, key, value) {
target[key] = value
console.log('reactive set', key, value)
if (callbacks[key] != undefined) {
for (const callback of callbacks[key]) {
callback()
}
}
}
})
}
let data = reactive(options.data())
const tplReg = /\{\{\s*(\w+)\s*\}\}/g
let render = function (ele, tpl) {
let html = tpl.replace(tplReg, function (str, key) {
console.log('render tpl', str, key)
return data[key];
}).trim()
console.log('html', tpl, html)
return html;
}
let update = function () {
for (let i = 0; i < dom.children.length; i++) {
const ele = dom.children[i];
for (let j = 0; j < ele.attributes.length; j++) {
const att = ele.attributes[j];
console.log('parse att', att.name, att.value)
if (att.name.startsWith('@')) {
const eventName = att.name.substring(1)
ele.addEventListener(eventName, function () {
let code = att.value;
keys.forEach(key => code = code.replace(key, 'data.'+key))
console.log('triger event', eventName, code)
eval(code)
})
}
}
let tpl = ele.innerText;
if (ele.childElementCount == 0 && tpl.match(tplReg)) {
ele.innerText.replace(tplReg, function (str, key) {
console.log('parse tpl', str, key)
if (callbacks[key] != undefined) {
callbacks[key].push(function () {
ele.innerText = render(ele, tpl);
});
}
})
}
ele.innerText = render(ele, ele.innerText)
}
}
return {
mount(selector) {
console.log('mount', selector)
dom = document.querySelector(selector)
update()
}
}
}
</script>
<div id="app">
<button @click="count++">
Count is: {{ count }}
</button>
<button @click="count++">
Count2 is: {{ count }}
</button>
</div>
<script>
createApp({
data() {
return {
count: 0
}
}
}).mount('#app')
</script>
</body>
</html>