-
Notifications
You must be signed in to change notification settings - Fork 107
/
package-style.html
60 lines (55 loc) · 1.52 KB
/
package-style.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>包装方式</title>
</head>
<body>
<script>
var body = document.querySelector('body'),
newNode = null,
append = function(str){
newNode = document.createElement("p");
newNode.innerHTML = str;
body.appendChild(newNode);
};
// 原对象
var historyModule = {
listener: [],
listen: function (listener) {
this.listener.push(listener);
append('historyModule listen.')
},
updateLocation: function(){
append('historyModule updateLocation tirgger.');
this.listener.forEach(function(listener){
listener('new localtion');
})
}
}
// Router 将使用 historyModule 对象,并对其包装
var Router = {
source: {},
init: function(source){
this.source = source;
},
listen: function(listener) {
append('Router listen.');
// 对 historyModule的listen进行了一层包装
return this.source.listen(function(location){
append('Router listen tirgger.');
listener(location);
})
}
}
// 将 historyModule 注入进 Router 中
Router.init(historyModule);
// Router 注册监听
Router.listen(function(location){
append(location + '-> Router setState.');
})
// historyModule 触发监听回调
historyModule.updateLocation();
</script>
</body>
</html>