-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
91 lines (78 loc) · 2.89 KB
/
index.html
File metadata and controls
91 lines (78 loc) · 2.89 KB
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
<!DOCTYPE html>
<html>
<body>
<input id="input" type="text">
<div id="result"></div>
<script type="text/javascript">
var Render = {
init: function(element, store){
this.element = element;
this.store = store;
window.addEventListener('render-view', this);
},
handleEvent: function(e){
switch(e.type){
case 'render-view':
this.element.textContent = this.store.get();
console.timeEnd("[diff] input vs render");
break;
}
}
};
function Store() {
this._data = 0;
}
Store.prototype = {
init: function(){
window.addEventListener('set-store',this);
},
handleEvent: function(e){
switch(e.type){
case 'set-store':
this.set(e.detail.val);
break;
}
},
get: function (val) {
return this._data;
},
set: function(val) {
this._data = val;
window.dispatchEvent(new CustomEvent('render-view'));
}
};
var Dispatcher = {
init: function(){
var self = this;
"keydown keyup input".split(" ").forEach(function(e){
window.addEventListener(e, self);
});
this.store = new Store();
this.store.init();
this.resultElem = document.getElementById('result');
this.inputElem = document.getElementById('input');
Render.init(this.resultElem, this.store);
},
handleEvent: function(e){
switch(e.type){
case 'input':
switch (e.target) {
case this.inputElem:
console.time("[diff] input vs render");
window.dispatchEvent(
new CustomEvent('set-store', {
detail: {
val: e.target.value
}
})
);
break;
}
break;
}
}
};
Dispatcher.init();
</script>
</body>
</html>