-
Notifications
You must be signed in to change notification settings - Fork 1
/
program-9.html
234 lines (209 loc) · 6.22 KB
/
program-9.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
<!DOCTYPE html>
<html>
<head>
<title>Program 9 - Releasing views</title>
<style>
body {
max-width: 30em;
margin-left: auto;
margin-right: auto;
margin-top: 4em;
}
div.buttons {
text-align: right;
}
</style>
</head>
<body>
</body>
<script type="text/javascript">
/*
Program 9 - Releasing views
All the examples so far have used a fixed set of views. Now we turn
to cases when views are created and deleted, and handling the
bookkeeping required to delete views without memory leaks.
Our example will be a simple wizard, taking the user through
a series of three screens. In production, you would probably
also show the current screen in the URL as we did for tabs,
but we will omit that here since what we are interested in is
how to cleanly delete views.
As usual, we begin with an observable, and a very simple model
containing the current view and a single text field that we
will set to a value input in a text box on the second screen
of the wizard.
*/
var Observable = {
addObserver: function(observer) {
this.observers.add(observer);
},
deleteObserver: function(observer) {
this.observers.delete(observer);
},
notify: function() {
this.observers.forEach(function(observer) {
observer.update();
});
}
};
var Model = Object.create(Observable, {
observers: { writable: true, value: new Set() },
name: { writable: true, value: '' },
});
Model.setName = function(name) {
this.name = name;
this.notify();
};
/*
As usual, we need a root view to contain our other views.
*/
var Body = {
subviews: new Set(),
addSubview: function(subview) {
this.subviews.add(subview);
},
show: function() {
currentScreen: { writeable: true }
var fragment = document.createDocumentFragment();
var div = document.createElement('div');
fragment.appendChild(div);
this.subviews.forEach(function(subview) {
var el = document.createElement('div');
div.appendChild(el);
subview.writeOver(el);
}, this);
document.body.appendChild(fragment);
}
};
/*
Next we define the three views. Each of them have the familiar
methods, but with a new one: release. Release is called when
a view is to be removed. It should stop the view from observing
any observables and clear any resources. It should *not* remove
the view from the DOM. That will be done by whatever is replacing
the view.
This is also why we have been using writeOver for views instead
of something like appendTo(parent). If we release and replace the
second view of five in a parent element, writeOver can do so
directly with no further bookkeeping. With appendTo, we would
have to somehow pass around information about where the view
fell among its siblings.
*/
var Screen0 = {
writeOver: function(el) {
this.div = document.createElement('div');
var p = document.createElement('p');
p.textContent = 'Enter your name on the next screen.';
this.div.appendChild(p);
var buttons = document.createElement('div');
buttons.setAttribute('class', 'buttons');
var next = document.createElement('button');
next.textContent = 'Next';
next.addEventListener('click', function() {
Model.addObserver(Screen1);
Screen1.writeOver(this.div);
this.release();
}.bind(this));
buttons.appendChild(next);
this.div.appendChild(buttons);
el.parentNode.replaceChild(this.div, el);
},
release: function() {
Model.deleteObserver(Screen0);
// We must release all references to DOM elements
// or the garbage collector will not be able to
// collect them, even though the are gone from the DOM.
this.div = null;
},
update: function() {
// Nothing to do here
}
};
var Screen1 = {
writeOver: function(el) {
this.div = document.createElement('div');
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('placeholder', 'Your name');
input.setAttribute('value', Model.name);
input.addEventListener('input', function() {
Model.setName(this.input.value);
}.bind(this));
this.div.appendChild(input);
this.input = input;
var buttons = document.createElement('div');
buttons.setAttribute('class', 'buttons');
var back = document.createElement('button');
back.textContent = 'Back';
back.addEventListener('click', function() {
Model.addObserver(Screen0);
Screen0.writeOver(this.div);
this.release();
}.bind(this));
buttons.appendChild(back);
var next = document.createElement('button');
next.textContent = 'Next';
next.addEventListener('click', function() {
Model.addObserver(Screen2);
Screen2.writeOver(this.div);
this.release();
}.bind(this));
buttons.appendChild(next);
this.div.appendChild(buttons);
el.parentNode.replaceChild(this.div, el);
},
release: function() {
Model.deleteObserver(this);
this.div = null;
this.input = null;
},
update: function() {
if (this.input) {
if (this.input.value !== Model.name) {
this.input.value = Model.name;
}
}
}
};
var Screen2 = {
writeOver: function(el) {
this.div = document.createElement('div');
var p = document.createElement('p');
p.appendChild(document.createTextNode('Hello, '));
var span = document.createElement('span');
span.textContent = Model.name;
this.span = span;
p.appendChild(span);
this.div.appendChild(p);
var buttons = document.createElement('div');
buttons.setAttribute('class', 'buttons');
var next = document.createElement('button');
next.textContent = 'Back';
next.addEventListener('click', function() {
Model.addObserver(Screen1);
Screen1.writeOver(this.div);
this.release();
}.bind(this));
buttons.appendChild(next);
this.div.appendChild(buttons);
el.parentNode.replaceChild(this.div, el);
},
release: function() {
Model.deleteObserver(this);
this.div = null;
this.span = null;
},
update: function() {
if (this.span) {
if (this.span.textContent !== Model.name) {
this.span.textContent = Model.name;
}
}
}
};
document.addEventListener('DOMContentLoaded', function() {
Model.addObserver(Screen0);
Body.addSubview(Screen0);
Body.show();
});
</script>
</html>