Skip to content

Commit c70ba2b

Browse files
committed
preparing to move elm(ish) code to elmish.md ... for #44
1 parent e7d542f commit c70ba2b

File tree

3 files changed

+96
-23
lines changed

3 files changed

+96
-23
lines changed

Diff for: elmish.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Elm(_ish_) ?
2+
3+
Elm(_ish_) is our **`Elm`** "_inspired_" `JavaScript` (**ES5**)
4+
front-end _micro_-framework.<sup>1</sup>
5+
6+
## _Why?_
7+
8+
The purpose of building Elm(_ish_) is _not_ to "_replace_" Elm
9+
or to create _yet another_ front-end JS framework!
10+
11+
The purpose of _separating_ the Elm(ish) functions into a "micro framework" is:
12+
**a)** to ***simplify*** the Todo List application code. <br />
13+
**b)** _demonstrate_ a ***re-useable*** (_fully-tested_)
14+
"**micro-framework**" that allows us to _practice_ the Elm Architecture.<br />
15+
**c)** get into the **mindset** of writing **tests _first_**
16+
and then the _least_ amount of code necessary to pass the test
17+
(_while meeting the acceptance criteria_).
18+
19+
20+
By the end of this exercise you will _understand_
21+
The Elm Architecture (TEA) _much better_ because we will be
22+
analysing, documenting, testing and writing each function required
23+
to build the fully functional "micro framework".
24+
25+
26+
<sup>1</sup><small>The reason for calling the micro-framework Elm(ish)
27+
is to emphasize that it is "**inspired by**" **`Elm`**.
28+
But the only thing Elm(ish) shares with Elm is the architecture "pattern".
29+
In all other respects Elm(ish) is a "poor imitation" and should _only_
30+
be used for learning purposes!
31+
To _truly_ appreciate the awesome elegance, simplicity, power
32+
and personal effectiveness of using Elm, there is **no substitute**
33+
for the "real thing".
34+
</small>
35+
36+
37+
38+
39+
You are already _familiar_ with the first few functions
40+
`mount` and `empty`

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
(function() { // scope elmish to prevent conflicts if used elsewhere
1+
(function() { // scope elm(ish) functions to prevent conflicts if used elsewhere
22
/**
33
* `empty` the contents of a given DOM element "node" (before re-rendering).
44
* This is the *fastest* way according to: stackoverflow.com/a/3955238/1148249
5-
* @param {Object} node the exact DOM node you want to empty
5+
* @param {Object} node the exact DOM node you want to empty the contents of.
66
* @example
77
* // returns true (once the 'app' node is emptied)
88
* const node = document.getElementById('app');
@@ -12,7 +12,7 @@ function empty(node) {
1212
while (node.lastChild) {
1313
node.removeChild(node.lastChild);
1414
}
15-
}
15+
} // this function produces a (DOM) "mutation" but has no other "side effects".
1616

1717
/**
1818
* `mount` mounts the app in the "root" DOM Element.
@@ -192,17 +192,17 @@ function ul (attrlist, childnodes) {
192192

193193
/**
194194
* route sets the hash portion of the URL in a web browser.
195-
* @param {Object} state - the current state of the application.
195+
* @param {Object} model - the current state of the application.
196196
* @param {String} title - the title of the "page" being navigated to
197197
* @param {String} hash - the hash (URL) to be navigated to.
198198
* @return {Object} new_state - state with hash updated to the *new* hash.
199199
* @example
200200
* // returns the state object with updated hash value:
201-
* var new_state = elmish.route(state, 'Active', '#/active');
201+
* var new_state = elmish.route(model, 'Active', '#/active');
202202
*/
203-
function route (state, title, hash) {
203+
function route (model, title, hash) {
204204
window.location.hash = hash;
205-
var new_state = JSON.parse(JSON.stringify(state)); // clone state
205+
var new_state = JSON.parse(JSON.stringify(model)); // clone model
206206
new_state.hash = hash;
207207
return new_state;
208208
}

Diff for: todo-list.md

+49-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1-
# Elm(ish) Todo List (TodoMVC) Mini App (TDD Tutorial!)
1+
# Elm(ish) Todo List (TodoMVC) Mini App (Real World TDD Tutorial!)
22

33
If you've made it this far, give yourself a pat on the back!
4-
You are about to "_level up_" your JavaScript and "TEA" skills!
4+
Your persistence is about to pay off as you
5+
"_level up_" _both_ your **`JavaScript`** and "TEA" skills!
56

67

78
## Why?
89

9-
_Consolidate_ your understanding of The Elm Architecture (TEA)
10-
by creating a "real world" _useable_ App.
10+
The _purpose_ of this **Todo List _mini_ project** is to
11+
_consolidate_ your understanding of The Elm Architecture (TEA)
12+
by creating a "real world" _useable_ App following a _strict_
13+
Documentation and Test Driven Development approach.
14+
15+
This will _show_ you that it's not only _possible_
16+
to write docs and tests _first_,
17+
you will see _first hand_ that **`code`** is **more concise**,
18+
well-documented and thus **_easier_ to maintain**
19+
and you will get your "work" done ***much faster***.
20+
21+
These are _foundational_ skills that will
22+
pay **_immediate_ returns** on the time invested,
23+
and will **`continue`** to **`return`** "**interest**"
24+
for as long as you write (_and people use your_) software!
25+
26+
> _It's **impossible** to "**over-state**" how **vital writing tests first**
27+
to both your **personal effectiveness** and **long-term sanity**.
28+
Thankfully, by the end of this page, you will see how **easy** it is._
29+
30+
1131

1232
## What?
1333

@@ -19,14 +39,14 @@ Along the way we will cover:
1939
+ [x] Browser Routing/Navigation
2040
+ [x] Local Storage for Offline Support
2141

22-
We will be abstracting all "TEA" related code
42+
We will be abstracting all "TEA" related ("generic framework") code
2343
into a file called **`elmish.js`**
24-
so that our Todo List application can be as simple
44+
so that our Todo List application can be as concise
2545
and "declarative" as possible.
2646

2747
### Todo List?
2848

29-
If you are _unfamiliar_ with Todo lists,
49+
If you are _unfamiliar_ with Todo lists, simply put:
3050
they are a way of keeping a list of the tasks that need to be done. <br />
3151
see: https://en.wikipedia.org/wiki/Time_management#Setting_priorities_and_goals
3252

@@ -38,17 +58,19 @@ Watch: https://www.youtube.com/results?search_query=checklist+manifesto
3858
### TodoMVC?
3959

4060
If you have not come across TodoMVC before,
41-
it's a sample application to showcase various "frontend" frameworks.
61+
it's a sample application to showcase various "frontend" frameworks
62+
using a common user interface (UI): a Todo List Application.
4263
![TodoMVC-intro](https://user-images.githubusercontent.com/194400/42624420-4528a3c6-85bd-11e8-8b92-9b1c8951ba35.png)
4364

4465

45-
We highly recommend checking out the following links:
66+
We _highly recommend_ checking out the following links:
4667

4768
+ Website: http://todomvc.com
4869
+ GitHub project: https://github.com/tastejs/todomvc
4970

50-
For our purposes we will simply be re-using the **`CSS`**
51-
to make our TEA Todo List _look_ nice.
71+
For our purposes we will simply be re-using the **TodoMVC `CSS`**
72+
to make our TEA Todo List _look_ good
73+
(_not have to "worry" about styles so we can **focus on functionality**_).
5274
All the JavaScript code will be written "_from scratch_"
5375
to ensure that everything is clear.
5476

@@ -79,13 +101,19 @@ please see:
79101
and
80102
[front-end-with-tape.md](https://github.com/dwyl/learn-tape/blob/master/front-end-with-tape.md)
81103

104+
105+
106+
82107
It's "OK" to ask: "_Where do I **start** (my **TDD** quest)?_" <br />
83108
The answer is: create **two** new files:
84109
`examples/todo-list/elmish.js` and `test/elmish.test.js`
85110

86111
We will create a couple of tests and their corresponding functions _next_!
87112

88-
## Elm(_ish_) ?
113+
114+
115+
116+
89117

90118
Our **first step** is to _abstract_ and _generalise_
91119
the Elm Architecture (`mount`) and HTML ("DOM") functions
@@ -151,8 +179,9 @@ test('empty("root") removes DOM elements from container', function (t) {
151179
```
152180

153181
> _**Note**: if any line in this file is **unfamiliar** to you,
154-
please **first** go back over the previous example(s),
155-
**then** do bit of "googling" for any words or functions you don't recognise
182+
please **first** go back over the previous example(s)_:
183+
`counter-basic` _and_ `counter-reset`,
184+
_**then** do bit of "googling" for any words or functions you don't recognise
156185
e.g: `childElementCount`,
157186
and if you are **still** "**stuck**"_,
158187
[***please open an
@@ -1264,7 +1293,7 @@ writing the "business logic" of the Todo List Application,
12641293
because it will "make more sense" in context._
12651294

12661295

1267-
### Elm(ish) Store > Save Model Data to `localStorage`
1296+
### Elm(ish) Store > Save Model (Data) to `localStorage`
12681297

12691298
The _final_ piece in the "Elm(ish)" puzzle is saving data on the device
12701299
so that the Todo List items (_and history_) is not "_lost_" when
@@ -1412,7 +1441,8 @@ test.only('elmish.mount sets model in localStorage', function (t) {
14121441
});
14131442
```
14141443

1415-
There is quite a lot to "unpack" in this test but let's break it down:
1444+
There is quite a lot to "unpack" in this test but let's walk through the steps:
1445+
14161446
1. Require the `view` and `update` from our counter reset example.
14171447
2. `mount` the counter reset app
14181448
3. ***test*** that the `model` (7) is being saved to `localStorage`
@@ -1428,6 +1458,9 @@ the initial model from `localStorage` if it is defined.
14281458
Based on this test, try to add the necessary lines to the `mount` function,
14291459
to make the test pass.
14301460

1461+
**`if`** you get stuck trying to make this test pass,
1462+
refer to the completed code:
1463+
[/examples/todo-list/elmish.js](https://github.com/dwyl/learn-elm-architecture-in-javascript/tree/master/examples/todo-list/elmish.js)
14311464

14321465

14331466
<!--

0 commit comments

Comments
 (0)