Skip to content

Commit 820250b

Browse files
committed
add section on learning style and three options for when people can read the elm(ish) docs/tutorial for #44
1 parent e1f9851 commit 820250b

File tree

3 files changed

+66
-61
lines changed

3 files changed

+66
-61
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ function empty(node) {
1414
}
1515
} // this function produces a (DOM) "mutation" but has no other "side effects".
1616

17+
18+
1719
/**
1820
* `mount` mounts the app in the "root" DOM Element.
1921
* @param {Object} model store of the application's state.
@@ -36,7 +38,6 @@ function mount(model, update, view, root_element_id) {
3638
localStorage.setItem('elmish_store', JSON.stringify(model)); // save model!
3739
}
3840

39-
4041
/**
4142
* `add_attributes` applies the desired attribute(s) to the specified DOM node.
4243
* Note: this function is "impure" because it "mutates" the node.

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

+9-49
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
(function() { // scope functions to prevent naming conflicts
2+
/* if require is available, it means we are in Node.js Land i.e. testing! */
3+
/* istanbul ignore next */
4+
if (typeof require !== 'undefined' ) { // require elm(ish) creating local copy
5+
const { a, button, div, empty, footer, input, h1, header, label, li, mount,
6+
route, section, span, strong, text, ul } = require('./elmish.js');
7+
} // in the browser elm(ish) functions are automatically be available
8+
19
// Define the Component's Actions:
210
var Inc = 'inc'; // increment the counter
311
var Dec = 'dec'; // decrement the counter
@@ -35,55 +43,7 @@ function view(signal, model, root) {
3543
}).forEach(function (el) { root.appendChild(el) }); // forEach is ES5 so IE9+
3644
}
3745

38-
// Mount Function receives all MUV and mounts the app in the "root" DOM Element
39-
function mount(model, update, view, root_element_id) {
40-
var root = document.getElementById(root_element_id); // root DOM element
41-
function signal(action) { // signal function takes action
42-
return function callback() { // and returns callback
43-
model = update(model, action); // update model according to action
44-
view(signal, model, root); // subsequent re-rendering
45-
};
46-
};
47-
view(signal, model, root); // render initial model (once)
48-
}
49-
50-
// The following are "Helper" Functions which each "Do ONLY One Thing" and are
51-
// used in the "View" function to render the Model (State) to the Browser DOM:
52-
53-
// empty the contents of a given DOM element "node" (before re-rendering)
54-
function empty(node) {
55-
while (node.firstChild) {
56-
node.removeChild(node.firstChild);
57-
}
58-
} // Inspired by: stackoverflow.com/a/3955238/1148249
5946

60-
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section
61-
function container(index, elements) {
62-
var con = document.createElement('section');
63-
con.id = index;
64-
con.className = 'counter';
65-
elements.forEach(function(el) { con.appendChild(el) });
66-
return con;
67-
}
6847

69-
function button(text, signal, action) {
70-
var button = document.createElement('button');
71-
var text = document.createTextNode(text); // human-readable button text
72-
button.appendChild(text); // text goes *inside* not attrib
73-
button.className = action.split('-')[0]; // use action as CSS class
74-
button.id = action;
75-
// console.log(signal, ' action:', action)
76-
button.onclick = signal(action); // onclick tells how to process
77-
return button; // return the DOM node(s)
78-
} // how to create a button in JavaScript: stackoverflow.com/a/8650996/1148249
7948

80-
function div(divid, text) {
81-
var div = document.createElement('div');
82-
div.id = divid;
83-
div.className = divid;
84-
if(text !== undefined) { // if text is passed in render it in a "Text Node"
85-
var txt = document.createTextNode(text);
86-
div.appendChild(txt);
87-
}
88-
return div;
89-
}
49+
})(); // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression

Diff for: todo-list.md

+55-11
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,31 @@ by opening a question you help _everyone_ learn more effectively!
9090

9191
### `Elm`(_ish_) ?
9292

93-
`Elm`(_ish_)
93+
In order to _simplify_ the code for our Todo List App,
94+
we _abstracted_ much of the "_generic_" code
95+
into a "front-end micro framework" called `Elm`(_ish_).
96+
The functions & functionality of `Elm`(_ish_) should be _familiar_ to you
97+
so you _should_ be able to build the Todo List using the `Elm`(_ish_)
98+
helper functions e.g: `mount`, `div`, `input` and `route`.
99+
100+
You can _opt_ to _either_:
101+
**a)** read the `Elm`(_ish_) docs/tutorial
94102
[`elmish.md`](https://github.com/dwyl/learn-elm-architecture-in-javascript/blob/master/elmish.md)
103+
***`before`*** building the Todo List App -
104+
this will give you both TDD practice
105+
and a deeper understanding of building a micro framework.
106+
i.e. "**_prospective_ learning**"<br />
107+
**b)** refer the `Elm`(_ish_) docs/tutorial
108+
[`elmish.md`](https://github.com/dwyl/learn-elm-architecture-in-javascript/blob/master/elmish.md)
109+
***`while`*** building the Todo List App when you "**_need_ to know**"
110+
how one of the helper functions works. i.e. "**_contextual_ learning**" <br />
111+
**c)** **only _consult_** the `Elm`(_ish_) docs/tutorial
112+
[`elmish.md`](https://github.com/dwyl/learn-elm-architecture-in-javascript/blob/master/elmish.md)
113+
***`if`*** you are "stuck" ***`while`*** building the Todo List App.
114+
i.e. "**_debug_ learning**" <br />
115+
116+
The choice is yours; there is no "right" way.
117+
95118

96119

97120
### Testing?
@@ -110,7 +133,7 @@ and
110133
[**front-end**-with-tape.md](https://github.com/dwyl/learn-tape/blob/master/front-end-with-tape.md)
111134

112135

113-
### Create Files
136+
#### Create Files
114137

115138
In your editor/terminal create the following files:
116139

@@ -123,7 +146,7 @@ Todo List App.
123146
`todo-app.js` is where all the JSDOCs and functions
124147
for our Todo List App will be written.
125148

126-
### Test Setup
149+
#### Test Setup
127150

128151
In order to run our test(s), we need some "setup" code
129152
that "requires" the libraries/files so we can _execute_ the functions.
@@ -153,30 +176,51 @@ you should see no output.
153176

154177

155178

179+
156180
### `model`
157181

158182
The `model` for our Todo List App is remarkably simple.
159-
All we need is an `Object` containing two keys:
183+
All we need is an `Object` containing two keys `todos` and `hash`:
160184

161185
```js
162186
{
163187
todos: [
164-
{ id: 1, title: "Learn Elm Architecture", completed: true },
165-
{ id: 2, title: "Build Todo List App", completed: false },
166-
{ id: 3, title: "Win the Internet!", completed, false }
188+
{ id: 1, title: "Learn Elm Architecture", done: true },
189+
{ id: 2, title: "Build Todo List App", done: false },
190+
{ id: 3, title: "Win the Internet!", done, false }
167191
],
168192
hash: '#/active' // the "route" to display
169193
}
170194
```
171195

196+
197+
172198
#### What about `metadata` ?
173199

174-
Rather than storing "metadata"
200+
> Metadata is a set of data that describes
201+
and gives information about other data.
202+
https://en.wikipedia.org/wiki/Metadata
203+
204+
There are two types of `metadata` in a Todo List App:
205+
+ `id` - each todo item has an `id`, this is the item's position in the list.
206+
+ count - the count of items according to their state of _completion_.
207+
e.g: in the model above there are
208+
2 items which are "active"
209+
and 1 which is "done".
210+
211+
Rather than _storing_ "metadata" in the model
175212
(_e.g: the count of active and completed Todo items_)
176-
we will "compute" (derive) what we _need_ at "runtime",
177-
this may "waste" a few CPU cycles, but that's "OK"!
213+
we will "compute" (derive) it "runtime" to keep the `model` simple.
214+
This may "waste" a few CPU cycles computing the count but that's "OK"!
178215
Even on an _ancient_ Android device
179-
this will only take a milisecond to compute
216+
this will only take a millisecond to compute,
217+
will not "slow down" the app or affect UX.
218+
219+
220+
221+
222+
223+
180224

181225

182226

0 commit comments

Comments
 (0)