Skip to content

Commit a19da15

Browse files
committed
adds sample implementation for update ADD case for #48
1 parent 4571db0 commit a19da15

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

Diff for: examples/todo-list/todo-app.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (typeof require !== 'undefined' ) { // require elm(ish) creating local copy
66
route, section, span, strong, text, ul } = require('./elmish.js');
77
} // in the browser elm(ish) functions are automatically be available
88

9+
910
var initial_model = {
1011
todos: [],
1112
hash: "#/"
@@ -19,23 +20,15 @@ var initial_model = {
1920
* @return {Object} updated_model - the transformed model.
2021
*/
2122
function update(action, model, data) {
22-
// console.log('model:', model);
2323
var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
24-
// console.log('new_model:', new_model);
2524
switch(action) { // and an action (String) runs a switch
26-
// case 'ADD':
27-
// new_model.todos.push({
28-
// id: model.todos.length + 1,
29-
// title: data,
30-
// done: false
31-
// });
32-
// break;
33-
// case Dec:
34-
// new_model.counters[index] = model.counters[index] - 1;
35-
// break;
36-
// case Res: // use ES6 Array.fill to create a new array with values set to 0:
37-
// new_model.counters[index] = 0;
38-
// break;
25+
case 'ADD':
26+
new_model.todos.push({
27+
id: model.todos.length + 1,
28+
title: data,
29+
done: false
30+
});
31+
break;
3932
default: // if action unrecognised or undefined,
4033
return model; // return model unmodified
4134
} // see: https://softwareengineering.stackexchange.com/a/201786/211301

Diff for: todo-list.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,39 @@ You should see the assertion _fail_:
511511
![update-add-item-test-failing](https://user-images.githubusercontent.com/194400/43639131-206b632c-9713-11e8-83ee-d0ecab0ac4ef.png)
512512

513513

514+
#### `ADD` item _Implementation_
514515

516+
With the above test as your "guide",
517+
write the _bare minimum_ code necessary to make all assertions pass.
515518

516-
#### `ADD` item _Implementation_
519+
_Sample_ implementation:
520+
```js
521+
/**
522+
* `update` transforms the `model` based on the `action`.
523+
* @param {String} action - the desired action to perform on the model.
524+
* @param {Object} model - the App's (current) model (or "state").
525+
* @param {String} data - the data we want to "apply" to the item.
526+
* @return {Object} updated_model - the transformed model.
527+
*/
528+
function update(action, model, data) {
529+
var new_model = JSON.parse(JSON.stringify(model)) // "clone" the model
530+
switch(action) { // and an action (String) runs a switch
531+
case 'ADD':
532+
new_model.todos.push({
533+
id: model.todos.length + 1,
534+
title: data,
535+
done: false
536+
});
537+
break;
538+
default: // if action unrecognised or undefined,
539+
return model; // return model unmodified
540+
} // see: https://softwareengineering.stackexchange.com/a/201786/211301
541+
return new_model;
542+
}
543+
```
544+
the `case 'ADD'` is the _relevant_ code. <br />
545+
546+
> Was _your_ implementation _similar_...?
517547
518548

519549

0 commit comments

Comments
 (0)