-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
276 lines (228 loc) · 6.75 KB
/
index.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
background: #f5f5f5;
color: #4d4d4d;
font-weight: 300;
}
</style>
<body>
<input id="myBtn" type="button" value="click"></input>
<span id="data"></span>
<script type="text/javascript">
//Make it global
//The previous example has some defaults. For example, observers(tom and jerry) need to know the name of the subject(store) so as to listen to a specific event. Besides, each subject(store) needs to maintain its own clientList.
//To solve that, we can create a middleware, which is a global Event system.
var Event = (function(){
//create a closure using IIFE(immediately-invoked function)
var clientList = {}, //clientList as a map
listen,
trigger,
remove;
listen = function(key, fn) {
if (!clientList[key]) {
clientList[key] = [];
}
clientList[key].push(fn);
};
trigger = function() {
var key = Array.prototype.shift.call(arguments),
fns = clientList[key];
if (!fns || fns.length === 0) {
return false;
}
for (var i = 0; i < fns.length; i++) {
var fn = fns[i];
fn.apply(this, arguments);
}
};
remove = function(key, fn) {
var fns = clientList[key];
if (!fns) {
return false;
}
if (!fn) {
fns && (fns.length = 0);
} else {
for (var l = fns.length; l >= 0; l--) {
if (fns[l] === fn) {
fns.splice(l, 1);
}
}
}
}
return {
listen: listen,
trigger: trigger,
remove: remove
}
})();
var store = {
"coke": 2.5,
"cheese": 2
}
var tom = {
name: "tom",
getCokePrice: function(price) {
console.log("coke price is " + price)
}
}
//now the subject and observer don't need to know each other
var eventName = "coke";
Event.listen("coke", tom.getCokePrice.bind(tom));
Event.trigger("coke", store["coke"]);
</script>
<script type="text/javascript">
// //Make the observer function as an object, so it can be applied to different subjects who implement it (like an interface in java)
// //we call it event object/interface
// var event = {
// clientList: [], //previously, the clientList is an property of a specific subject
// listen: function(key, fn) {
// //key is the type of the event, fn is the callback from observer
// if (!this.clientList[key]) {
// this.clientList[key] = [];
// }
// this.clientList[key].push(fn);
// }, //the listen function is the same as the addClient
// trigger: function() {
// var key = Array.prototype.shift.call(arguments);
// fns = this.clientList[key];
// if (!fns || fns.length == 0) {
// return false;
// }
// for (var i = 0; i < fns.length; i++) {
// var fn = fns[i];
// fn.apply(this, arguments); //yes, we apply 'this' when trigger the observer's callback. 'this' means the subject here. That's why when the button trigger the event, you will find the the callback function from listener refers 'this' to the button!!!
// }
// },
// remove: function(key, fn) {
// //a method to stop a callback from observer
// //get the callback list corresponding to the key
// var fns = this.clientList[key];
// console.log("here");
// console.log(fns);
// if (!fns || fns.length == 0) {
// return false;
// }
// if (!fn) {
// //if no specifc call is defined, delete all the callbacks corresponding to the key
// fns && (fns.length = 0); //ensure fns still exit at this point
// } else {
// for (var l = fns.length - 1; l >=0; l--) {
// //backward check
// var _fn = fns[l];
// if ( _fn === fn) {
// console.log("find it", fn, fns[l]);
// fns.splice(l, 1);
// }
// }
// }
// }
// }
// //define an installEvent function to implement the event obj, which is similar to "implements" in java
// function installEvent(obj) {
// //get all properties of event obj
// for (var key in event) {
// obj[key] = event[key];
// }
// }
// //Test
// var store = {};
// installEvent(store);
// //observer, here we call it Client
// function Client(name) {
// this.name = name;
// }
// Client.prototype.getPrice = function(price) {
// console.log(this.name + " get price " + price);
// }
// var tom = new Client("Tom");
// var jerry = new Client("Jerry");
// //add observer
// store.listen("cheese", jerry.getPrice.bind(jerry));
// store.listen("coke", fn1 = tom.getPrice.bind(tom));
// //notify the observer
// store.trigger("coke", 2);
// store.trigger("cheese", 2.5);
// store.remove("coke", fn1);
// store.trigger("coke", 200);
</script>
<script type="text/javascript">
// //A little More
// //Subject
// var store = {};
// //a cache to store the observers(the callbacks from them)
// store.clientList = []; //notice that this is an array of array since we want to let observer listen to different types of events.
// //function to add observer , the "listen" function
// store.addClient = function(key, fn) {
// //use key to differentiate different events(news)
// if (!this.clientList[key]) {
// this.clientList[key] = [];
// }
// this.clientList[key].push(fn);
// }
// //notify the observers when sth happened
// store.trigger = function() {
// //from the arguments, get the type of the event!
// var key = Array.prototype.shift.call(arguments);
// //get the array of observer callbacks corresponding to that event
// var fns = this.clientList[key];
// if (!fns || fns.length == 0) {
// return false;
// }
// for (var i = 0, fn; i < fns.length; i++) {
// fn = fns[i];
// fn.apply(this, arguments);
// }
// }
// //observer, here we call it Client
// function Client(name) {
// this.name = name;
// }
// Client.prototype.getPrice = function(price) {
// console.log(this.name + " receives price is " + price);
// }
// var tom = new Client("Tom");
// var jerry = new Client("Jerry");
// //add observer
// store.addClient("coke", tom.getPrice.bind(tom));
// store.addClient("cheese", jerry.getPrice.bind(jerry));
// //notify the observer
// store.trigger("coke", 2);
// store.trigger("cheese", 2.5);
// //notice the difference from callback
// (function success() {
// data = {
// coke: 2,
// cheese, 2.5
// }
// tom.getPrice(data["coke"]);
// jerry.getPrice(data["cheese"]);
// })();
</script>
<script type="text/javascript">
// //The Basic
// //the subject
// var subject = document.querySelector("#myBtn");
// //create observers
// var Observer = function(name) {
// this.name = name;
// }
// Observer.prototype.notify = function() {
// console.log(this.name + " receives notification");
// }
// var obj1 = new Observer("obj1");
// var obj2 = new Observer("obj2");
// var testObj = {
// name: "hello world",
// notify: function() {
// console.log(this.name + " obj notified");
// }
// }
// //observe
// subject.addEventListener("click", obj1.notify.bind(obj1));
// subject.addEventListener("click", obj2.notify.bind(obj2));
// subject.addEventListener("click", testObj.notify.bind(testObj));
</script>