@@ -182,11 +182,100 @@ please see:
182
182
[ https://github.com/dwyl/**learn-jsdoc ** ] ( https://github.com/dwyl/learn-jsdoc )
183
183
184
184
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
+
185
244
### _ Analyse_ the TodoMVC App to "Gather Requirements"
186
245
187
- Once we have a
188
246
Our _ next_ step is to _ analyse_ the required functionality of a Todo List.
189
247
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
+
190
279
### Todo List _ Basic_ Functionality
191
280
192
281
A todo list has only 2 basic functions:
@@ -232,34 +321,6 @@ is ~~a great~~ the best way to _understand_ it. <br />
232
321
This is the "physics" approach. see: https://youtu.be/L-s_3b5fRd8?t=22m37s
233
322
234
323
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
-
263
324
### HTML Elements (Functions)
264
325
265
326
The _ requirements_ for the HTML elements we _ need_ for a Todo List
0 commit comments