-
Notifications
You must be signed in to change notification settings - Fork 0
/
yue.js
123 lines (104 loc) · 2.5 KB
/
yue.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
// 创建vue实例
// new Yue({
// data() {
// return {
// msg: "hello yangkai"
// }
// },
// })
class Yue {
constructor(options) {
this.$options = options;
this.$data = options.data;
this.$methods = options.methods;
this.observer(this.$data);
this.proxyData(this.$data);
this.proxyMethod(this.$methods);
// 测试dep和watcher
// new Watcher(this, 'test')
// console.log('this.test',this.test);
// new Watcher(this, 'foo')
// console.log('this.foo.bar', this.foo.bar);
new Compile(this, options.el);
if (typeof options.created === 'function') {
options.created.call(this);
}
}
observer(data) {
// console.log('observer', data);
// 数据不存在或者不是对象,则无法监听
if (!data || typeof data !== "object") return;
Object.keys(data).forEach(key => {
this.defineReactive(data, key, data[key]);
});
}
defineReactive(data, key, value) {
// console.log('defineReactive', key, value);
this.observer(value);
const dep = new Dep();
Object.defineProperty(data, key, {
get() {
Dep.target && dep.addDep(Dep.target);
return value;
},
set(newValue) {
if (value === newValue) return;
value = newValue;
// console.log(key + '的value 更新了:', value);
dep.notify();
}
})
}
proxyData(data) {
// console.log('proxyData', data);
if (!data || typeof data !== "object") return;
Object.keys(data).forEach(key => {
Object.defineProperty(this, key, {
get() {
return this.$data[key];
},
set(val) {
this.$data[key] = val;
}
})
});
}
proxyMethod(methods) {
Object.keys(methods).forEach(name => {
Object.defineProperty(this, name, {
get() {
return this.$methods[name];
},
set(val) {
this.$methods[name] = val;
}
})
});
}
}
class Dep {
constructor() {
this.deps = [];
}
addDep(dep) {
this.deps.push(dep)
}
notify() {
this.deps.forEach(dep => dep.update());
}
}
class Watcher {
constructor(vm, key, cb) {
this.vm = vm;
this.key = key;
this.cb = cb;
// 每创建一个Watcher实例时,都将该实例赋值给Dep.target,在get中可以用到
Dep.target = this;
this.vm[this.key];
Dep.target = null;
}
update() {
// console.log(`属性${this.key}更新了`);
this.cb.call(this.vm, this.vm[this.key]);
}
}