-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.js
136 lines (118 loc) · 3.56 KB
/
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 这个 我的 vue实例
class Vue {
constructor(options){
this.$data = options.data
// 为date对象上的属性添加数据劫持
Observe(this.$data)
// 调用属性代理
Obagent(this, this.$data)
// dom渲染
Compile(options.el, this)
}
}
// 收集发布订阅
class Dep {
constructor(){
this.subs = []
}
addSub(watcher){
this.subs.push(watcher)
}
notify(){
this.subs.forEach(watcher => watcher.update())
}
}
// 订阅者
class Watcher {
constructor(vm, key, callback){
this.vm = vm
this.key = key
this.callback = callback
Dep.target = this
key.split('.').reduce((newObj, key) => newObj[key], vm)
Dep.target = null
}
update(){
const value = this.key.split('.').reduce((newObj, key) => newObj[key], this.vm)
this.callback(value)
}
}
function Observe(obj) {
if(!obj || typeof obj !== 'object') return
const dep = new Dep()
Object.keys(obj).forEach(key => {
let value = obj[key]
Observe(value)
Object.defineProperty(obj,key,{
enumerable:true,
configurable:true,
get(){
Dep.target && dep.addSub(Dep.target)
return value
},
set(newVal){
value = newVal
Observe(value)
dep.notify()
}
})
})
}
function Obagent(_this, obj){
Object.keys(obj).forEach(key => {
Object.defineProperty(_this, key, {
configurable:true,
enumerable:true,
get(){
return obj[key]
},
set(newVal){
obj[key] = newVal
}
})
})
}
function Compile(el, vm) {
vm.$el = document.querySelector(el)
const fragment = document.createDocumentFragment()
while (childNode = vm.$el.firstChild){
fragment.appendChild(childNode)
}
replace(fragment)
vm.$el.appendChild(fragment)
function replace(node){
const regMsg = /\{\{\s*(\S+)\s*\}\}/
if(node.nodeType === 3){
const text = node.textContent
const execResult = regMsg.exec(text)
if(execResult){
const value = execResult[1].split('.').reduce((newObj,key) => newObj[key], vm)
node.textContent = text.replace(regMsg, value)
new Watcher(vm, execResult[1], newValue => {
node.textContent = text.replace(regMsg, newValue)
})
}
return
}
if(node.nodeType === 1 && node.tagName.toUpperCase() === 'INPUT'){
const findResult = Array.from(node.attributes).find(attr => attr.name === 'v-model')
if(findResult){
const text = findResult.textContent
const newVal = text.split('.').reduce((newObj,key) => newObj[key], vm)
node.value = newVal
new Watcher(vm, text, newValue => {
node.value = newValue
})
node.addEventListener('input', newValue => {
const value = newValue.target.value
const keys = text.split('.')
const newKeys = keys.slice(0, keys.length - 1)
const obj = newKeys.reduce((newObj, key) => newObj[key], vm)
obj[keys.at(-1)] = value
})
}
return
}
node.childNodes.forEach(child => replace(child))
}
}