Skip to content

Commit 2e67605

Browse files
committed
add solution to routing all/active/completed using footer links including window.onhashchange
1 parent fc2ed3c commit 2e67605

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

examples/todo-list/todo-app.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function update(action, model, data) {
2121
// console.log(arguments)
2222
// console.log(' - - - - - - - - - - - ');
2323
var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
24+
2425
switch(action) {
2526
case 'ADD':
2627
var last = (typeof model.todos !== 'undefined' && model.todos.length > 0)
@@ -106,14 +107,16 @@ function update(action, model, data) {
106107
return !item.done; // only return items which are item.done = false
107108
});
108109
break;
110+
case 'ROUTE':
111+
new_model.hash = (window && window.location && window.location.hash) ?
112+
window.location.hash : '#/';
113+
break;
109114
default: // if action unrecognised or undefined,
110115
return model; // return model unmodified
111116
} // see: https://softwareengineering.stackexchange.com/a/201786/211301
112117
return new_model;
113118
}
114119

115-
116-
117120
/**
118121
* `render_item` creates an DOM "tree" with a single Todo List Item
119122
* using the "elmish" DOM functions (`li`, `div`, `input`, `label` and `button`)
@@ -181,7 +184,18 @@ function render_main (model, signal) {
181184
label(["for=toggle-all"], [ text("Mark all as complete") ]),
182185
ul(["class=todo-list"],
183186
(model.todos && model.todos.length > 0) ?
184-
model.todos.map(function (item) {
187+
model.todos
188+
.filter(function (item) {
189+
switch(model.hash) {
190+
case '#/active':
191+
return !item.done;
192+
case '#/completed':
193+
return item.done;
194+
default:
195+
return item;
196+
}
197+
})
198+
.map(function (item) {
185199
return render_item(item, model, signal)
186200
}) : null
187201
) // </ul>
@@ -229,13 +243,25 @@ function render_footer (model, signal) {
229243
]),
230244
ul(["class=filters"], [
231245
li([], [
232-
a(["href=#/", "class=selected"], [text("All")])
246+
a([
247+
"href=#/", "id=all", "class=" +
248+
(model.hash === '#/' ? "selected" : '')
249+
],
250+
[text("All")])
233251
]),
234252
li([], [
235-
a(["href=#/active"], [text("Active")])
253+
a([
254+
"href=#/active", "id=active", "class=" +
255+
(model.hash === '#/active' ? "selected" : '')
256+
],
257+
[text("Active")])
236258
]),
237259
li([], [
238-
a(["href=#/completed"], [text("Completed")])
260+
a([
261+
"href=#/completed", "id=completed", "class=" +
262+
(model.hash === '#/completed' ? "selected" : '')
263+
],
264+
[text("Completed")])
239265
])
240266
]), // </ul>
241267
button(["class=clear-completed", "style=display:" + display_clear,
@@ -265,6 +291,7 @@ function render_footer (model, signal) {
265291
* var DOM = view(model);
266292
*/
267293
function view (model, signal) {
294+
268295
return (
269296
section(["class=todoapp"], [ // array of "child" elements
270297
header(["class=header"], [
@@ -295,7 +322,7 @@ function subscriptions (signal) {
295322
var ESCAPE_KEY = 27; // used for "escaping" when editing a Todo item
296323

297324
document.addEventListener('keyup', function handler (e) {
298-
// console.log('e.keyCode:', e.keyCode, '| key:', e.key);
325+
console.log('e.keyCode:', e.keyCode, '| key:', e.key);
299326

300327
switch(e.keyCode) {
301328
case ENTER_KEY:
@@ -316,6 +343,11 @@ function subscriptions (signal) {
316343
break;
317344
}
318345
});
346+
347+
window.onhashchange = function route () {
348+
console.log("signal('ROUTE')()");
349+
signal('ROUTE')();
350+
}
319351
}
320352

321353
/* module.exports is needed to run the functions using Node.js for testing! */

0 commit comments

Comments
 (0)