-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvue-core.html
244 lines (213 loc) · 6.78 KB
/
vue-core.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<h2>vue 深层原理剖析</h2>
<script>
//
function cb(val) {
/* 渲染视图 */
console.log('视图更新啦~')
}
function defineReactive(obj, key, val) {
Object.defineProperty(obj, key, {
enumerable: true /* 属性可枚举 */,
configurable: true /* 属性可被修改或删除 */,
get: function reactiveGetter() {
return val /* 实际上会依赖收集,下一小节会讲 */
},
set: function reactiveSetter(newVal) {
if (newVal === val) return
cb(newVal)
},
})
}
function observer(value) {
if (!value || typeof value !== 'object') {
return
}
/* Object.keys() 遍历获取所有对象的key值 是数组形式 */
Object.keys(value).forEach((key) => {
defineReactive(value, key, value[key])
})
}
// /* es6 类方法 constructor 方法 里面写 constructor属性 注意区分开 */
// class Vue {
// /* Vue构造类 */
// constructor(options) {
// this._data = options.data;
// observer(this._data);
// }
// }
// let o = new Vue({
// data: {
// test: "I am test."
// }
// })
// /* 修改数据进行触发 */
// o._data.test = '我是不是成功了'
// -------------- 依赖收集 ---------------
/* 订阅者 */
class Dep {
constructor() {
/* 用来存放Watcher对象的数组 */
this.subs = []
}
/* 在subs中添加一个Watcher对象 */
addSub(sub) {
this.subs.push(sub)
}
/* 通知所有Watcher对象更新视图 */
notify() {
this.subs.forEach((sub) => {
sub.update()
})
}
}
/* 观察者 */
class Watcher {
constructor() {
/* 在new一个Watcher对象时将该对象赋值给Dep.target,在get中会用到 */
Dep.target = this
}
/* 更新视图的方法 */
update() {
console.log('视图更新啦~')
}
}
Dep.target = null
function defineReactive(obj, key, val) {
/* 一个Dep类对象 */
const dep = new Dep()
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter() {
/* 将Dep.target(即当前的Watcher对象存入dep的subs中) */
dep.addSub(Dep.target)
return val
},
set: function reactiveSetter(newVal) {
if (newVal === val) return
/* 在set的时候触发dep的notify来通知所有的Watcher对象更新视图 */
dep.notify()
},
})
}
class Vue {
constructor(options) {
this._data = options.data
observer(this._data)
/* 新建一个Watcher观察者对象,这时候Dep.target会指向这个Watcher对象 */
new Watcher()
/* 在这里模拟render的过程,为了触发test属性的get函数 */
console.log('render~', this._data.test)
}
}
const vm = new Vue({
data: {
test: '我的为什么不可以了呢',
},
})
vm._data.test = '我的花花世界 '
let ress = {
visitedTime: 1524883051784,
content: '',
url: 'http://dbcenter.ingageapp.com/mysql/query#',
data: '2018-12-01',
time: '09:12:50',
}
var jsonData = {
status: 0,
data: {
visitor: {
browserType: {
value: 1,
label_resourceKey: 'XdMDPickOptn.serviceVisitor.browserType.1',
label: 'Chrome',
},
osType: {
value: 1,
label_resourceKey: 'XdMDPickOptn.serviceVisitor.osType.1',
label: 'Windows',
},
pageSource: {},
sourcePage: '',
},
userTrackDetails: [
{
visitedTime: 1525383051784,
content: 'dbcenter主页',
url: 'http://dbcenter.ingageapp.com/mysql/query',
},
{
visitedTime: 1525283051784,
content: 'dbcenter主页',
url: 'http://dbcenter.ingageapp.com/mysql/query',
},
{
visitedTime: 1525183051784,
content: 'dbcenter主页',
url: 'http://dbcenter.ingageapp.com/mysql/query',
},
{
visitedTime: 1525083051784,
content: '',
url: 'http://dbcenter.ingageapp.com/mysql/query#',
},
{
visitedTime: 1524983051784,
content: '',
url: 'http://dbcenter.ingageapp.com/mysql/query#',
},
{
visitedTime: 1524883051784,
content: '',
url: 'http://dbcenter.ingageapp.com/mysql/query#',
},
],
},
msg: '成功',
}
function changeTime(val) {
val = Number(val)
const date = new Date(val)
let year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours(),
minute = date.getMinutes(),
second = date.getSeconds()
month = month.toString().padStart(2, '0')
day = day.toString().padStart(2, '0')
hour = hour.toString().padStart(2, '0')
minute = minute.toString().padStart(2, '0')
second = second.toString().padStart(2, '0')
return {
date: `${year}-${month}-${day}`,
time: `${hour}:${minute}:${second}`,
}
}
const time = jsonData.data.userTrackDetails
const obj = {}
for (let item of time) {
let visitedTime = item.visitedTime
let res = changeTime(visitedTime)
Object.assign(item, res)
obj[res.date] = []
for (let val of time) {
if (visitedTime == val.visitedTime) {
obj[res.date].push(val)
}
}
}
console.log(obj)
console.log(jsonData)
</script>
</body>
</html>