@@ -50,6 +50,7 @@ Along the way we will cover:
50
50
+ [x] The Document Object Model (DOM)
51
51
+ [x] Browser Routing/Navigation
52
52
+ [x] Local Storage for Offline Support
53
+ + [x] Keyboard event listeners for rapid todo list creation and editing!
53
54
54
55
We will be abstracting all "TEA" related ("generic") code
55
56
into a file called ** ` elmish.js ` **
@@ -72,7 +73,7 @@ Watch: https://www.youtube.com/results?search_query=checklist+manifesto
72
73
### TodoMVC?
73
74
74
75
If you have not come across TodoMVC before,
75
- it's a sample application to showcase various "frontend" frameworks
76
+ it's a website that showcases various "frontend" frameworks
76
77
using a common user interface (UI): a Todo List Application.
77
78
![ TodoMVC-intro] ( https://user-images.githubusercontent.com/194400/42624420-4528a3c6-85bd-11e8-8b92-9b1c8951ba35.png )
78
79
@@ -137,9 +138,9 @@ The choice is yours; there is no "_right_" way to learn.
137
138
_ Before_ diving into _ building_ the Todo List App,
138
139
we need to consider how we are going to _ test_ it.
139
140
By ensuring that we follow ** TDD** from the _ start_ of an App,
140
- we will have [ "** no surprises"] ( https://youtu.be/u5CVsCnxyXg )
141
+ we will have [ "*** no surprises*** "] ( https://youtu.be/u5CVsCnxyXg )
141
142
and _ avoid_ having to "correct" any
142
- [ "** bad habits** "] ( https://www.youtube.com/results?search_query=Destiny%27s+Child+Bad+Habit ) .
143
+ [ "*** bad habits* ** "] ( https://www.youtube.com/results?search_query=Destiny%27s+Child+Bad+Habit ) .
143
144
144
145
We will be using ** Tape** and ** JSDOM** for testing
145
146
both our functions and the final application.
@@ -156,6 +157,7 @@ In your editor/terminal create the following files:
156
157
157
158
+ ` test/todo-app.test.js `
158
159
+ ` examples/todo-list/todo-app.js `
160
+ + ` examples/todo-list/index.html `
159
161
160
162
These file names should be self-explanatory, but if unclear,
161
163
` todo-app.test.js ` is where we will write the tests for our
@@ -188,25 +190,24 @@ and
188
190
[ ** front-end** -with-tape.md] ( https://github.com/dwyl/learn-tape/blob/master/front-end-with-tape.md )
189
191
190
192
If you attempt to run the test file: ` node test/todo-app.test.js `
191
- you should see no output.
193
+ you should see no output. < br />
192
194
(_ this is expected as we haven't written any tests yet!_ )
193
195
194
196
195
197
196
198
### ` model `
197
199
198
200
The ` model ` for our Todo List App is ** _ boringly_ simple** .
199
- All we need is an ` Object ` containing two keys ` todos ` and ` hash ` :
201
+ All we need is an ` Object ` with a
202
+ ` todos ` key which has an Array of Objects as it's value:
200
203
201
204
``` js
202
205
{
203
206
todos: [
204
207
{ id: 1 , title: " Learn Elm Architecture" , done: true },
205
208
{ id: 2 , title: " Build Todo List App" , done: false },
206
209
{ id: 3 , title: " Win the Internet!" , done, false }
207
- ],
208
- hash: ' #/' , // the "route" to display
209
- editing: false // if we are editing a todo list item this will be set.
210
+ ]
210
211
}
211
212
```
212
213
` todos ` is an ` Array ` of ` Objects ` and each Todo (Array) item
282
283
* and as the "reset" state when all todos are deleted at once.
283
284
*/
284
285
var initial_model = {
285
- todos: [],
286
- hash: " #/"
286
+ todos: [], // empty array which we will fill shortly
287
+ hash: " #/" // the hash in the url (for routing)
287
288
}
288
289
289
290
/* module.exports is needed to run the functions using Node.js for testing! */
@@ -328,12 +329,13 @@ The **`JSDOC`** for our `update` function is:
328
329
329
330
As with the ` update ` in our ` counter ` example
330
331
the function body is a ` switch ` statement
331
- that "decides" how to handle a request based on the ` action ` (_ also known as the "message"_ ).
332
+ that "decides" how to handle a request based on the ` action `
333
+ (_ also known as the "message"_ ).
332
334
333
335
Given that we _ know_ that our ` update ` function "skeleton"
334
336
will be a ` switch ` statement
335
337
(_ because that is the "TEA" pattern_ )
336
- good test to _ start_ with is the ` default case ` .
338
+ a good test to _ start_ with is the ` default case ` .
337
339
338
340
Append following test code in ` test/todo-app.test.js ` :
339
341
@@ -358,10 +360,13 @@ You should see the assertion _fail_:
358
360
Write the _ minimum_ code necessary to pass the test.
359
361
360
362
> Yes, we could just write:
363
+
361
364
``` js
362
365
function update (action , model ) { return model; }
363
366
```
367
+
364
368
And that _ would_ make the test _ pass_ . <br />
369
+
365
370
But, in light of the fact that we ** know** the ` update `
366
371
function body will contain a ` switch ` statement,
367
372
make the test pass by returning the ` model ` _ unmodified_ in the ` default ` case.
@@ -595,17 +600,15 @@ _FROM_:
595
600
{
596
601
todos: [
597
602
{id: 1 , " Toggle a todo list item" , done: false }
598
- ],
599
- hash: " #/"
603
+ ]
600
604
}
601
605
```
602
606
_ TO_ :
603
607
``` js
604
608
{
605
609
todos: [
606
610
{id: 1 , " Toggle a todo list item" , done: true }
607
- ],
608
- hash: " #/"
611
+ ]
609
612
}
610
613
```
611
614
@@ -653,8 +656,8 @@ If you get "stuck" see: [**`todo-app.js`**](https://github.com/dwyl/learn-elm-ar
653
656
654
657
#### Hold On, Does This Work _ Both_ Ways?
655
658
656
- _ Yes_ , You _ guessed_ it,
657
- choosing to name the ` action ` as "` TOGGLE ` "
659
+ _ Yes_ , you _ guessed_ it!
660
+ Choosing to name the ` action ` as "` TOGGLE ` "
658
661
is _ precisely_ because we don't _ need_
659
662
to have a _ ** separate** _ function
660
663
to "undo" an item if it has been "checked off".
@@ -727,7 +730,8 @@ whereas the second and third items are still "todo" (`done=false`).
727
730
728
731
This is what this ` model ` looks like in the "VanillaJS"
729
732
TodoMVC:
730
- echo ![ todomvc-3-items-1-done] ( https://user-images.githubusercontent.com/194400/43689907-e9caa548-98f8-11e8-8fd1-7b63e7fc5e30.png )
733
+
734
+ ![ todomvc-3-items-1-done] ( https://user-images.githubusercontent.com/194400/43689907-e9caa548-98f8-11e8-8fd1-7b63e7fc5e30.png )
731
735
732
736
Our _ quest_ in the next "pomodoro" is to re-create this
733
737
using the DOM functions we created in ` Elm ` (_ ish_ )!
@@ -742,6 +746,7 @@ and _just_ focus on rendering the _list_ itself.
742
746
743
747
In your web browser, open ** Dev** eloper ** Tools**
744
748
and _ inspect_ the HTML for the Todo list:
749
+ http://todomvc.com/examples/vanillajs/
745
750
746
751
![ todomvc-main-section-todo-list-html] ( https://user-images.githubusercontent.com/194400/43717480-9fb80982-997f-11e8-9ffe-6aa90a89a042.png )
747
752
@@ -791,7 +796,7 @@ with a **`class="view"`** which "wraps":
791
796
(_ which updates the model
792
797
From: ` model.todos[id].done=false `
793
798
To: ` model.todos[id].done=true ` _ )
794
- + [ ] ** ` <label> ` ** - the text content of the todo list item
799
+ + [ ] ** ` <label> ` ** - the text content ("title") of the todo list item
795
800
+ [ ] ** ` <button class="destroy"> ` ** - the button the person
796
801
can click/tap to ** ` delete ` ** a Todo item.
797
802
@@ -852,17 +857,18 @@ Given the test above, I added the following code to my `todo-app.js` file:
852
857
``` js
853
858
/* if require is available, it means we are in Node.js Land i.e. testing! */
854
859
/* istanbul ignore next */
855
- const { a , button , div , empty , footer , input , h1 , header , label , li , mount ,
856
- route , section , span , strong , text , ul } =
857
- (typeof require !== ' undefined' ) ? require (' ./elmish.js' ) : {};
860
+ if (typeof require !== ' undefined' && this .window !== this ) {
861
+ var { a, button, div, empty, footer, input, h1, header, label, li, mount,
862
+ route, section, span, strong, text, ul } = require (' ./elmish.js' );
863
+ }
858
864
859
865
/**
860
866
* `render_item` creates an DOM "tree" with a single Todo List Item
861
867
* using the "elmish" DOM functions (`li`, `div`, `input`, `label` and `button`)
862
868
* returns an `<li>` HTML element with a nested `<div>` which in turn has the:
863
- * + `<input type=checkbox>` which lets users to "Toggle" the status of the item
864
- * + `<label>` which displays the Todo item text (`title`) in a `<text>` node
865
- * + `<button class="destroy">` lets people "delete" a todo item.
869
+ * `<input type=checkbox>` which lets users to "Toggle" the status of the item
870
+ * `<label>` which displays the Todo item text (`title`) in a `<text>` node
871
+ * `<button class="destroy">` lets people "delete" a todo item.
866
872
* see: https://github.com/dwyl/learn-elm-architecture-in-javascript/issues/52
867
873
* @param {Object} item the todo item object
868
874
* @return {Object} <li> DOM Tree which is nested in the <ul>.
@@ -968,18 +974,20 @@ Onwards!
968
974
<br />
969
975
970
976
971
- ### ` <footer> ` Element [ issues/53] ( https://github.com/dwyl/learn-elm-architecture-in-javascript/issues/53 )
977
+ ### ` <footer> ` Element [ issues/53] ( https://github.com/dwyl/learn-elm-architecture-in-javascript/issues/53 )
972
978
973
979
Referring again to the _ rendered_ HTML
974
980
on http://todomvc.com/examples/vanillajs as our "guide":
975
- ![ image] ( https://user-images.githubusercontent.com/194400/42633421-5eb20f24-85d8-11e8-94ad-bb653dd93ab0.png )
981
+
982
+ ![ footer-screenshot] ( https://user-images.githubusercontent.com/194400/42633421-5eb20f24-85d8-11e8-94ad-bb653dd93ab0.png )
983
+
976
984
there is:
977
- + [ ] a ** ` <footer> ` ** element with
978
- + [ ] a ** ` <span> ` ** element which contains
979
- + [ ] a ** ` text ` ** node with: ** "` {count} ` item(s) left"** .
980
- + [ ] a ** ` <ul> ` ** containing
981
- + [ ] 3 ** ` <li> ` ** elements each with
982
- + [ ] a link (** ` <a> ` ** ) which allow the "user"
985
+ + [ ] ** ` <footer> ` ** element with:
986
+ + [ ] ** ` <span> ` ** element which contains
987
+ + [ ] ** ` text ` ** node with: ** "` {count} ` item(s) left"** .
988
+ + [ ] ** ` <ul> ` ** containing
989
+ + [ ] 3 x ** ` <li> ` ** elements each with
990
+ + [ ] link (** ` <a> ` ** ) which allow the "user"
983
991
to *** filter*** which items appear in the ** ` <view> ` ** .
984
992
+ [ ] a ** ` <button class="clear-completed"> ` **
985
993
which will ** _ Clear_ all ` Completed ` ** items when clicked.
@@ -1046,7 +1054,7 @@ from building the previous view functions
1046
1054
** ` render_item ` ** and ** ` render_main ` **
1047
1055
we *** estimate*** with _ reasonable confidence_
1048
1056
that it will take us
1049
- ** _ 25 minutes** (_ ** one** "** pomodoro** _ )
1057
+ ** 25 minutes** (_ ** one** "** pomodoro** _ )
1050
1058
to:
1051
1059
+ [ ] Craft the ** ` JSDOC ` ** comment _ documenting_ the ` render_footer ` function
1052
1060
so that all future developers will _ easily_ understand what the function does.
@@ -1143,8 +1151,8 @@ Given the docs and test above, attempt to write the `render_footer` function.
1143
1151
with what happens when the "Clear completed" **`<buton> `** is clicked/tapped.
1144
1152
We will "cover" that below. For now, focus on rendering the DOM._
1145
1153
1146
- > If you get "stuck" trying to make the tests pass, first keep tring!
1147
- Then "as a friend" and finally, consult the _ reference_ implementation in:
1154
+ > If you get "stuck" trying to make the tests pass, first keep trying! < br />
1155
+ Then "ask a friend" and finally, consult the _ reference_ implementation in:
1148
1156
[ ** ` todo-app.js ` ** ] ( https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/commits/68e2afa3bd95c46da7df559007d90dedcbae500f#diff-6be3e16fe7cfb4c00788d4d587374afdR103 )
1149
1157
1150
1158
@@ -1279,8 +1287,8 @@ you will see something like this ("_Red_"):
1279
1287
You should have the knowledge & skill
1280
1288
to write the ` view ` function and make the test pass.
1281
1289
1282
- > If you get "stuck" trying to make the tests pass, first keep tring!
1283
- Then "as a friend" and finally, consult the _ reference_ implementation in:
1290
+ > If you get "stuck" trying to make the tests pass, first keep trying! < br />
1291
+ Then "ask a friend" and finally, consult the _ reference_ implementation in:
1284
1292
[ ** ` todo-app.js ` ** ] ( https://github.com/dwyl/learn-elm-architecture-in-javascript/pull/45/commits/3096d81a777392c07a132136db496224871ff4c9#diff-6be3e16fe7cfb4c00788d4d587374afdR145 )
1285
1293
1286
1294
When you run ` npm test ` you should see something like this:
0 commit comments