@@ -11,8 +11,14 @@ var initial_model = {
11
11
hash : "#/"
12
12
}
13
13
14
- function update ( model , action ) { // Update function takes the current state
15
- var new_model = JSON . parse ( JSON . stringify ( model ) ) // "clone" the model
14
+ /**
15
+ * `update` transforms the `model` based on the `action`.
16
+ * @param {String } action - the desired action to perform on the model.
17
+ * @param {Object } model - the App's data ("state").
18
+ * @return {Object } updated_model - the transformed model.
19
+ */
20
+ function update ( action , model ) { // Update function takes the current state
21
+ // var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
16
22
switch ( action ) { // and an action (String) runs a switch
17
23
// case 'CREATE':
18
24
// new_model.counters[index] = model.counters[index] + 1;
@@ -23,30 +29,31 @@ function update(model, action) { // Update function takes the current state
23
29
// case Res: // use ES6 Array.fill to create a new array with values set to 0:
24
30
// new_model.counters[index] = 0;
25
31
// break;
26
- default : return model ; // if action not defined, return curent state.
27
- }
28
- return new_model ;
32
+ default : // if action unrecognised or undefined,
33
+ return model ; // return model unmodified
34
+ } // see: https://softwareengineering.stackexchange.com/a/201786/211301
35
+ // return new_model;
29
36
}
30
37
31
- function view ( signal , model , root ) {
32
- empty ( root ) ; // clear root element before re-rendering the App (DOM).
33
- model . counters . map ( function ( counter , index ) {
34
- return container ( index , [ // wrap DOM nodes in an "container"
35
- button ( '+' , signal , Inc + '-' + index ) , // append index to action
36
- div ( 'count' , counter ) , // create div w/ count as text
37
- button ( '-' , signal , Dec + '-' + index ) , // decrement counter
38
- button ( 'Reset' , signal , Res + '-' + index ) // reset counter
39
- ] ) ;
40
- } ) . forEach ( function ( el ) { root . appendChild ( el ) } ) ; // forEach is ES5 so IE9+
41
- }
38
+ // function view(signal, model, root) {
39
+ // empty(root); // clear root element before re-rendering the App (DOM).
40
+ // model.counters.map(function(counter, index) {
41
+ // return container(index, [ // wrap DOM nodes in an "container"
42
+ // button('+', signal, Inc + '-' + index), // append index to action
43
+ // div('count', counter), // create div w/ count as text
44
+ // button('-', signal, Dec + '-' + index), // decrement counter
45
+ // button('Reset', signal, Res + '-' + index) // reset counter
46
+ // ]);
47
+ // }).forEach(function (el) { root.appendChild(el) }); // forEach is ES5 so IE9+
48
+ // }
42
49
43
50
/* module.exports is needed to run the functions using Node.js for testing! */
44
51
/* istanbul ignore next */
45
52
if ( typeof module !== 'undefined' && module . exports ) {
46
53
module . exports = {
47
54
model : initial_model ,
48
55
update : update ,
49
- view : view
56
+ // view: view
50
57
}
51
58
}
52
59
0 commit comments