Skip to content

Commit 560c9bc

Browse files
committed
adds instructions+code for mount generic function in elm(ish) for #44
1 parent 28ab679 commit 560c9bc

File tree

1 file changed

+90
-29
lines changed

1 file changed

+90
-29
lines changed

todo-list.md

Lines changed: 90 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,100 @@ please see:
182182
[https://github.com/dwyl/**learn-jsdoc**](https://github.com/dwyl/learn-jsdoc)
183183

184184

185+
### `mount` the App
186+
187+
The next function we need for "TEA" is `mount`.
188+
189+
In the `test/elmish.test.js` file, type the following code:
190+
```js
191+
// use view and update from counter-reset example
192+
// to invoke elmish.mount() function and confirm it is generic!
193+
const { view, update } = require('../examples/counter-reset/counter.js');
194+
195+
test('elmish.mount app expect state to be Zero', function (t) {
196+
const root = document.getElementById(id);
197+
elmish.mount(7, update, view, id);
198+
const actual = document.getElementById(id).textContent;
199+
const actual_stripped = parseInt(actual.replace('+', '')
200+
.replace('-Reset', ''), 10);
201+
const expected = 7;
202+
t.equal(expected, actual_stripped, "Inital state set to 7.");
203+
// reset to zero:
204+
const btn = root.getElementsByClassName("reset")[0]; // click reset button
205+
btn.click(); // Click the Reset button!
206+
const state = parseInt(root.getElementsByClassName('count')[0]
207+
.textContent, 10);
208+
t.equal(state, 0, "State is 0 (Zero) after reset."); // state reset to 0!
209+
elmish.empty(root); // clean up after tests
210+
t.end()
211+
});
212+
```
213+
> _**Note**: we have "**borrowed**" this test from our previous example.
214+
see:_ `test/counter-reset.test.js`
215+
216+
The corresponding code for the `mount` function
217+
in `examples/todo-list/elmish.js` is:
218+
```js
219+
/**
220+
* `mount` mounts the app in the "root" DOM Element.
221+
* @param {Object} model store of the application's state.
222+
* @param {Function} update how the application state is updated ("controller")
223+
* @param {Function} view function that renders HTML/DOM elements with model.
224+
* @param {String} root_element_id root DOM element in which the app is mounted
225+
*/
226+
function mount(model, update, view, root_element_id) {
227+
var root = document.getElementById(root_element_id); // root DOM element
228+
function signal(action) { // signal function takes action
229+
return function callback() { // and returns callback
230+
var updatedModel = update(model, action); // update model for the action
231+
empty(root); // clear root el before rerender
232+
view(signal, updatedModel, root); // subsequent re-rendering
233+
};
234+
};
235+
view(signal, model, root); // render initial model (once)
236+
}
237+
```
238+
239+
240+
Now that we have started creating the `elmish` generic functions,
241+
we need to know which _other_ functions we need.
242+
Let's take a look at the TodoMVC App to see what we need.
243+
185244
### _Analyse_ the TodoMVC App to "Gather Requirements"
186245

187-
Once we have a
188246
Our _next_ step is to _analyse_ the required functionality of a Todo List.
189247

248+
### _Recommended_ Background Reading: TodoMVC "_Vanilla_" JS
249+
250+
By _far_ the best place to start for _understanding_ TodoMVC's layout/format,
251+
is the "Vanilla" JavaScript (_no "framework"_) implementation:
252+
https://github.com/tastejs/todomvc/tree/gh-pages/examples/vanillajs
253+
254+
Run it locally with:
255+
```
256+
git clone https://github.com/tastejs/todomvc.git
257+
cd todomvc/examples/vanillajs
258+
python -m SimpleHTTPServer 8000
259+
```
260+
Open your web browser to: http://localhost:8000
261+
262+
![vanillajs-localhost](https://user-images.githubusercontent.com/194400/42632838-6e68c20c-85d6-11e8-8ae4-d688f5977704.png)
263+
264+
_Play_ with the app by adding a few items,
265+
checking-off and toggling the views in the footer.
266+
267+
> _**Note**: IMO the "**Vanilla**" **JS** implementation
268+
is quite complex and insufficiently documented_
269+
(_very few code comments and sparse_
270+
[`README.md`](https://github.com/tastejs/todomvc/tree/25a9e31eb32db752d959df18e4d214295a2875e8/examples/vanillajs)),
271+
_so don't expect to **understand** it all the first time without study._
272+
_Don't worry, we will walk through building each feature in detail._
273+
274+
> If you are unable to run the TodoMVC locally, you can always view it online:
275+
>> Please add direct link here!
276+
277+
278+
190279
### Todo List _Basic_ Functionality
191280

192281
A todo list has only 2 basic functions:
@@ -232,34 +321,6 @@ is ~~a great~~ the best way to _understand_ it. <br />
232321
This is the "physics" approach. see: https://youtu.be/L-s_3b5fRd8?t=22m37s
233322

234323

235-
### _Recommended_ Background Reading: TodoMVC "_Vanilla_" JS
236-
237-
By _far_ the best place to start for _understanding_ TodoMVC's layout/format,
238-
is the "Vanilla" JavaScript (_no "framework"_) implementation:
239-
https://github.com/tastejs/todomvc/tree/gh-pages/examples/vanillajs
240-
241-
Run it locally with:
242-
```
243-
git clone https://github.com/tastejs/todomvc.git
244-
cd todomvc/examples/vanillajs
245-
python -m SimpleHTTPServer 8000
246-
```
247-
Open your web browser to: http://localhost:8000
248-
249-
![vanillajs-localhost](https://user-images.githubusercontent.com/194400/42632838-6e68c20c-85d6-11e8-8ae4-d688f5977704.png)
250-
251-
_Play_ with the app by adding a few items,
252-
checking-off and toggling the views in the footer.
253-
254-
> _**Note**: IMO the "**Vanilla**" **JS** implementation
255-
is quite complex and insufficiently documented_
256-
(_very few code comments and sparse_
257-
[`README.md`](https://github.com/tastejs/todomvc/tree/25a9e31eb32db752d959df18e4d214295a2875e8/examples/vanillajs)),
258-
_so don't expect to **understand** it all the first time without study._
259-
_Don't worry, we will walk through building each feature in detail._
260-
261-
262-
263324
### HTML Elements (Functions)
264325

265326
The _requirements_ for the HTML elements we _need_ for a Todo List

0 commit comments

Comments
 (0)