@@ -410,21 +410,18 @@ 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> ` HTML Element
413
+ When building a House we don't think "build house" as our _ first_ action.
414
+ Instead we think: what are the "foundations" that need to be in place
415
+ *** before*** we lay the first brick?
414
416
415
- The _ first_ HTML element we encounter in the TodoMVC app is
416
- ` <section> ` .
417
- ` <section> ` represents a standalone section — which doesn't have
418
- a more specific semantic element to represent it —
419
- it's an alternative way to group elements to a ` <div> ` .
417
+ In our Todo List App we need a few "Helper Functions"
418
+ before we start building the App.
420
419
421
- > info: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section <br />
422
- > difference:
423
- https://stackoverflow.com/questions/6939864/what-is-the-difference-between-section-and-div
420
+ ### HTML / DOM Creation Generic Helper Functions
424
421
425
- As with other "grouping" or "container" HTML elements,
426
- our ` section ` function ( _ which will create the _ ` <section> ` _ DOM node _ )
427
- will be a function with _ two _ arguments:
422
+ All "grouping" or "container" HTML elements e.g: ` <div> ` or ` <section> `
423
+ will be called with *** two arguments *** :
424
+ e.g: ` var sec = section(attributes, childnodes) `
428
425
+ ` attributes ` - a list (Array) of HTML attributes/properties
429
426
e.g: ` id ` or ` class ` .
430
427
+ ` childnodes ` - a list (Array) of child HTML elements
@@ -433,11 +430,6 @@ will be a function with _two_ arguments:
433
430
Each of these function arguments will be "_ applied_ " to the HTML element.
434
431
We therefore need a pair of "helper" functions (_ one for each argument_ ).
435
432
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 )
440
-
441
433
442
434
### ` add_attributes `
443
435
@@ -475,37 +467,57 @@ test('elmish.add_attributes applies class HTML attribute to a node', function (t
475
467
476
468
Given the code in the test above,
477
469
take a moment to think of how _ you_ would write,
478
- the ` attributes ` function to apply a CSS ` class ` to an element. <br />
470
+ the ` add_attributes ` function to apply a CSS ` class ` to an element. <br />
479
471
Note: we have _ seen_ the code _ before_ in the ` counter ` example.
480
472
The difference is this time we want it to be "generic";
481
473
we want to apply a CSS ` class ` to _ any_ DOM node.
482
474
483
475
If you can, make the test _ pass_
484
- by writing the ` attributes ` function.
476
+ by writing the ` add_attributes ` function.
485
477
(_ don't forget to_ ` export ` _ the function at the end of the file_ ).
486
478
487
479
If you get "stuck", checkout:
488
480
https://github.com/dwyl/learn-elm-architecture-in-javascript/tree/master/examples/todo-list/elmish.js <br />
489
481
490
482
491
-
492
-
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;
483
+ > ** Note** : The ` add_attributes ` function is "impure" as it "mutates"
484
+ the target DOM ` node ` , this is more of a "fact of life" in JavaScript,
485
+ and given that the application of attributes
486
+ to DOM node(s) is idempotent we aren't "concerned" with "side effects";
496
487
the attribute will only be applied _ once_ to the node
497
- regardless of how many times the ` attributes ` function is called.
488
+ regardless of how many times the ` add_attributes ` function is called.
498
489
see: https://en.wikipedia.org/wiki/Idempotence
499
490
500
491
501
- For reference, the Elm HTML Attributes package is:
492
+ For reference, the Elm HTML Attributes function on Elm package is:
502
493
http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html-Attributes
503
494
504
- ### ` append_childnodes `
495
+ #### Input Placeholder Attribute
496
+
497
+ The ` <input> ` form element (_ where we create new Todo List items_ )
498
+ has a helpful ` placeholder ` _ prompting_ us with a question:
499
+ "_ What needs to be done?_ "
500
+
501
+ Add the following test to the ` test/elmish.test.js ` file: <br />
502
+
503
+ ``` js
504
+ test .only (' elmish.add_attributes set placeholder on <input> element' , function (t ) {
505
+ const root = document .getElementById (id);
506
+ let input = document .createElement (' input' );
507
+ input .id = ' new-todo' ;
508
+ input = elmish .add_attributes ([" placeholder=What needs to be done?" ], input);
509
+ root .appendChild (input);
510
+ const placeholder = document .getElementById (' new-todo' )
511
+ .getAttribute (" placeholder" );
512
+ t .equal (placeholder, " What needs to be done?" , " paceholder set on <input>" );
513
+ t .end ();
514
+ });
515
+ ```
505
516
506
517
518
+ ### ` append_childnodes `
507
519
508
- The ` append_childnodes ` functionality is a one-liner : <br />
520
+ The ` append_childnodes ` _ functionality _ is a " _ one-liner _ " : <br />
509
521
``` js
510
522
childnodes .forEach (function (el ) { parent .appendChild (el) });
511
523
```
@@ -557,6 +569,27 @@ If you get "stuck", checkout:
557
569
https://github.com/dwyl/learn-elm-architecture-in-javascript/tree/master/examples/todo-list/elmish.js <br />
558
570
559
571
572
+ ### ` <section> ` HTML Element
573
+
574
+ The _ first_ HTML element we encounter in the TodoMVC app is
575
+ ` <section> ` .
576
+ ` <section> ` represents a standalone section — which doesn't have
577
+ a more specific semantic element to represent it —
578
+ it's an alternative way to group elements to a ` <div> ` .
579
+
580
+ > info: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section <br />
581
+ > difference:
582
+ https://stackoverflow.com/questions/6939864/what-is-the-difference-between-section-and-div
583
+
584
+
585
+
586
+
587
+
588
+
589
+ Section in Elm: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html
590
+ <br />
591
+ Demo: https://ellie-app.com/LTtNVQjfWVa1
592
+ ![ ellie-elm-section] ( https://user-images.githubusercontent.com/194400/42708957-bbcc1020-86d6-11e8-97bf-f2f3a1c6fdea.png )
560
593
561
594
562
595
0 commit comments