Skip to content

Commit 1df145f

Browse files
committed
fix: keep search state ephemeral
1 parent 614e97d commit 1df145f

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/todo-app.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@ if (typeof require !== 'undefined' && this.window !== this) {
55
route, section, span, strong, text, ul } = require('./elmish.js');
66
}
77

8-
var initial_model = {
8+
function set_search_state(model, value) {
9+
Object.defineProperty(model, 'search', {
10+
value: value || '',
11+
writable: true,
12+
configurable: true,
13+
enumerable: false
14+
});
15+
return model;
16+
}
17+
18+
function clone_model(model) {
19+
var cloned = JSON.parse(JSON.stringify(model));
20+
return set_search_state(cloned, model && model.search);
21+
}
22+
23+
var initial_model = set_search_state({
924
todos: [],
1025
hash: "#/"
11-
}
26+
}, "")
1227

1328
/**
1429
* `update` transforms the `model` based on the `action`.
@@ -18,7 +33,7 @@ var initial_model = {
1833
* @return {Object} new_model - the transformed model.
1934
*/
2035
function update(action, model, data) {
21-
var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
36+
var new_model = clone_model(model) // "clone" the model
2237

2338
switch(action) {
2439
case 'ADD':
@@ -104,7 +119,7 @@ function update(action, model, data) {
104119
window.location.hash // : '#/';
105120
break;
106121
case 'SEARCH':
107-
new_model.search = data;
122+
set_search_state(new_model, data);
108123
break;
109124
default: // if action unrecognised or undefined,
110125
return model; // return model unmodified

0 commit comments

Comments
 (0)