@@ -410,9 +410,9 @@ This is a "copy-paste" of the _generated_ code including the Todo items:
410
410
Let's split each one of these elements into it's own ` function `
411
411
(_ with any necessary "helpers"_ ) in the order they appear.
412
412
413
- #### ` <section> `
413
+ ### ` <section> ` HTML Element
414
414
415
- The _ first_ HTML we encounter in the TodoMVC app is
415
+ The _ first_ HTML element we encounter in the TodoMVC app is
416
416
` <section> ` .
417
417
` <section> ` represents a standalone section — which doesn't have
418
418
a more specific semantic element to represent it —
@@ -427,40 +427,98 @@ our `section` function (_which will create the_ `<section>` _DOM node_)
427
427
will be a function with _ two_ arguments:
428
428
+ ` attributes ` - a list (Array) of HTML attributes/properties
429
429
e.g: ` id ` or ` class ` .
430
- + ` children ` - a list (Array) of child HTML elements
430
+ + ` childnodes ` - a list (Array) of child HTML elements
431
431
(_ nested within the_ ` <section> ` )
432
432
433
433
Each of these function arguments will be "_ applied_ " to the HTML element.
434
434
We therefore need a pair of "helper" functions (_ one for each argument_ ).
435
435
436
+ Section in Elm: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html
437
+ <br />
438
+ Demo: https://ellie-app.com/LTtNVQjfWVa1
439
+ ![ ellie-elm-section] ( https://user-images.githubusercontent.com/194400/42708957-bbcc1020-86d6-11e8-97bf-f2f3a1c6fdea.png )
436
440
437
441
438
- #### ` attributes `
442
+ ### ` attributes `
439
443
440
- ` attributes(attrlist, node) `
444
+ The ` JSDOC ` comment for our ` attributes ` function is:
445
+ ``` js
446
+ /**
447
+ * attributes applies the desired attributes to the desired node.
448
+ * Note: this function is "impure" because it "mutates" the node.
449
+ * however it is idempotent; the "side effect" is only applied once.
450
+ * @param {Array.<String>} attrlist list of attributes to be applied to the node
451
+ * @param {Object} node DOM node upon which attribute(s) should be applied
452
+ * @example
453
+ * // returns node with attributes applied
454
+ * div = attributes ([" class=item" , " id=mydiv" , " active=true" ], div);
455
+ */
456
+ ```
457
+ This should give you a _ good idea_ of what code needs to be written.
441
458
442
- The ` attributes ` function is "impure" as it "mutates"
443
- the target DOM ` node ` , however the application of attributes
444
- to DOM node(s) is idempotent;
445
- the attribute will only be applied _ once_ to the node
446
- regardless of how many times the ` attributes ` function is called.
447
- see: https://en.wikipedia.org/wiki/Idempotence
459
+ But let's write the _ test_ first!
460
+ Add the following test to the ` test/elmish.test.js ` file: <br />
461
+
462
+ ``` js
463
+ test (' elmish.attributes applies class HTML attribute to a node' , function (t ) {
464
+ const root = document .getElementById (id);
465
+ let div = document .createElement (' div' );
466
+ div .id = ' divid' ;
467
+ div = elmish .attributes ([" class=apptastic" ], div);
468
+ root .appendChild (div);
469
+ // test the div has the desired class:
470
+ const nodes = document .getElementsByClassName (' apptastic' );
471
+ t .equal (nodes .length , 1 , " <div> has 'apptastic' class applied" );
472
+ t .end ();
473
+ });
474
+ ```
448
475
476
+ Given the code in the test above,
477
+ take a moment to think of how _ you_ would write,
478
+ the ` attributes ` function to apply a CSS ` class ` to an element. <br />
479
+ Note: we have _ seen_ the code _ before_ in the ` counter ` example.
480
+ The difference is this time we want it to be "generic";
481
+ we want to apply a CSS ` class ` to _ any_ DOM node.
449
482
483
+ If you can, make the test _ pass_
484
+ by writing the ` attributes ` function.
485
+ (_ don't forget to_ ` export ` _ the function at the end of the file_ ).
450
486
487
+ If you get "stuck", checkout:
488
+ https://github.com/dwyl/learn-elm-architecture-in-javascript/tree/master/examples/todo-list/elmish.js <br />
451
489
452
- Section in Elm: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html
453
- <br />
454
- Demo: https://ellie-app.com/LTtNVQjfWVa1
455
- ![ ellie-elm-section] ( https://user-images.githubusercontent.com/194400/42708957-bbcc1020-86d6-11e8-97bf-f2f3a1c6fdea.png )
456
490
457
491
458
492
459
- The Elm HTML Attributes package is:
493
+ > The ` attributes ` function is "impure" as it "mutates"
494
+ the target DOM ` node ` , however the application of attributes
495
+ to DOM node(s) is idempotent;
496
+ the attribute will only be applied _ once_ to the node
497
+ regardless of how many times the ` attributes ` function is called.
498
+ see: https://en.wikipedia.org/wiki/Idempotence
499
+
500
+
501
+ For reference, the Elm HTML Attributes package is:
460
502
http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html-Attributes
461
503
504
+ ### ` childnodes `
462
505
506
+ The ` childnodes ` functionality is a one-liner: <br />
507
+ ``` js
508
+ childnodes .forEach (function (el ){ parent .appendChild (el) });
509
+ ```
510
+ It's easy to think: "_ why bother to create a_ ` function ` ...?" <br />
511
+ The _ reasons_ to create _ small_ functions are: <br />
512
+ ** a)** Keep the _ functionality_ "DRY" https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
513
+ which means we can _ easily_ track down all instances of function invocation.
514
+ <br />
515
+ ** b)** If we ever need to modify the function, e.g: to performance optimise it, there is a _ single_ definition.
516
+ <br />
517
+ ** c)** It makes unit-testing the functionality easy;
518
+ that's _ great_ news for reliability!
463
519
520
+ With that in mind, let's write a _ test_ for the ` childnodes ` function!
521
+ Add the following code to the ` test/elmish.test.js ` file: <br />
464
522
465
523
466
524
0 commit comments