-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgvue.js
184 lines (162 loc) · 3.28 KB
/
gvue.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* 重写Array push方法
* */
const methodsToPatch = [
'push',
'pop',
'shift',
'unshift',
'splice',
'sort',
'reverse'
]
function def(obj, key, val, enumerable) {
Object.defineProperty(obj, key, {
value: val,
enumerable: !!enumerable,
writable: true,
configurable: true
})
}
const arrayProto = Array.prototype
const arrayMethods = Object.create(arrayProto)
methodsToPatch.forEach(method => {
def(arrayMethods, method, function() {
let args = [],
len = arguments.length
while (len--) args[len] = arguments[len]
arrayProto[method].apply(this, args)
let inserted
switch (method) {
case 'push':
case 'unshift':
inserted = args
break
case 'splice':
inserted = args.slice(2)
break
}
// console.log('dep', this.__ob__.dep)
this.__ob__.dep.notify()
// if (inserted) {
// observeArray(this, inserted)
// }
})
})
function defineReactive(obj, key, val) {
observe(val)
const dep = new Dep()
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get() {
console.log(`get ${key} value: ${val}`)
Dep.target && dep.addDep(Dep.target)
if (Dep.target) {
if (Array.isArray(val)) {
val.__ob__.dep.addDep(Dep.target)
} else {
dep.addDep(Dep.target)
}
}
return val
},
set(newVal) {
console.log(`set ${key} value: ${newVal}`)
if (newVal !== val) {
val = newVal
dep.notify()
}
if (newVal !== null && typeof newVal === 'object') {
defineReactive(obj, key, newVal)
}
}
})
}
class Observer {
constructor(data) {
if (Array.isArray(data)) {
this.dep = new Dep()
def(data, '__ob__', this)
data.__proto__ = arrayMethods
} else {
this.walk(data)
}
}
walk(data) {
Object.keys(data).forEach(key => {
defineReactive(data, key, data[key])
})
}
}
function isObject(obj) {
return obj !== null && typeof obj === 'object'
}
function observe(obj) {
if (obj === null || !isObject(obj)) {
return
}
new Observer(obj)
}
function proxy(vm, data) {
Object.keys(data).forEach(key => {
Object.defineProperty(vm, key, {
get() {
return data[key]
},
set(newVal) {
data[key] = newVal
}
})
})
}
class GVue {
constructor(options) {
/*
options = {
el: '#app',
data: {
counter: 1,
desc: '<span style="color:red">kvue可还行?</span>'
}
}
*/
this.$options = options
// 拿出里面的data做observe
this.$data = options.data
this.$el = document.querySelector(options.el)
// 劫持数据
observe(this.$data)
proxy(this, this.$data)
// compile模板
const compile = new Compile(this, this.$el)
compile.compile()
}
}
class Dep {
constructor(vm) {
this.deps = []
this.$vm = vm
}
addDep(dep) {
this.deps.push(dep)
}
notify() {
this.deps.forEach(dep => {
dep.update()
})
}
}
class Watcher {
constructor(vm, exp, updateFn) {
this.$vm = vm
this.$exp = exp
this.$updateFn = updateFn
Dep.target = this
vm[exp]
Dep.target = null
}
update() {
this.$updateFn && this.$updateFn()
}
}