3
3
Learn how to build web applications using
4
4
the Elm ("Model Update View") Architecture in "_ plain_ " JavaScript.
5
5
6
+ [ ![ Build Status] ( https://travis-ci.org/dwyl/learn-elm-architecture-in-javascript.svg?branch=master )] ( https://travis-ci.org/dwyl/learn-elm-architecture-in-javascript )
7
+ [ ![ codecov] ( https://codecov.io/gh/dwyl/learn-elm-architecture-in-javascript/branch/master/graph/badge.svg )] ( https://codecov.io/gh/dwyl/learn-elm-architecture-in-javascript )
8
+ [ ![ dependencies Status] ( https://david-dm.org/dwyl/learn-elm-architecture-in-javascript/status.svg )] ( https://david-dm.org/dwyl/learn-elm-architecture-in-javascript )
9
+ [ ![ devDependencies Status] ( https://david-dm.org/dwyl/learn-elm-architecture-in-javascript/dev-status.svg )] ( https://david-dm.org/dwyl/learn-elm-architecture-in-javascript?type=dev )
10
+
11
+
6
12
> We think Elm is the _ future_ of Front End Web Development <br />
7
13
for all the _ reasons_ described in:
8
14
[ github.com/dwyl/** learn-elm#why** ] ( https://github.com/dwyl/learn-elm#why ) <br />
@@ -53,7 +59,7 @@ When compared to _other_ ways of organizing your code,
53
59
"MUV" has the following benefits:
54
60
+ Easier to _ understand_ what is going on in more advanced apps
55
61
because the "_ flow_ " is always the same.
56
- + *** Uni-directional data-flow*** means "state" is always predictable:
62
+ + *** Uni-directional data-flow*** means "state" of the app is always predictable;
57
63
given a specific starting "state" and sequence of update actions
58
64
the output/end state will _ always_ be the same. This makes testing/testability
59
65
very easy!
@@ -68,7 +74,7 @@ very easy!
68
74
69
75
Anyone who knows a _ little_ bit of JavaScript
70
76
and wants to learn how to organize/structure <br />
71
- their code/app in the most _ sane_ , predictable and testable way.
77
+ their code/app in a _ sane_ , predictable and testable way.
72
78
73
79
### _ Prerequisites_ ?
74
80
@@ -99,7 +105,7 @@ is often referred to as the application's `state`
99
105
+ ** U** pdate - how your app handles ` actions ` performed
100
106
by people and ` update ` the ` state ` of your.
101
107
+ ** V** iew - what the people using your app can see;
102
- a way to ` view ` your state as ` HTML `
108
+ a way to ` view ` the Model (counter) as ` HTML `
103
109
104
110
![ elm-muv-architecture-diagram] ( https://cloud.githubusercontent.com/assets/194400/25773775/b6a4b850-327b-11e7-9857-79b6972b49c3.png )
105
111
@@ -166,24 +172,21 @@ The mount function "wires up" the app and tells the _view_
166
172
how to process a ` signal ` sent by the user/client.
167
173
168
174
``` js
169
- // Mount Function receives all MUV and mounts the app in the "root" node
170
- function mount (muv , id ) { // state is encapsulated by mount function
171
- var root = document .getElementById (id); // get ref to root DOM element once
172
- var update = muv .update ; // make local copies of the init parameters
173
- var state = muv .model ; // initial state
174
- var view = muv .view ; // view is what renders the UI in Browser
175
-
175
+ function mount (model , update , view , root_element_id ) {
176
+ var root = document .getElementById (root_element_id); // root DOM element
176
177
function signal (action ) { // signal function takes action
177
178
return function callback () { // and returns callback
178
- state = update (state , action); // update state according to action
179
- view (signal, state , root); // subsequent re-rendering
179
+ model = update (model , action); // update model according to action
180
+ view (signal, model , root); // subsequent re-rendering
180
181
};
181
182
};
182
- view (signal, state , root); // render initial state (once)
183
+ view (signal, model , root); // render initial model (once)
183
184
}
184
185
```
186
+
185
187
` mount ` receives an ` Object ` containing three properties:
186
- + ` model ` : "_ initial state_ " of your application.
188
+ + ` model ` : "_ initial state_ " of your application
189
+ (_ in this case the counter which starts at 0_ )
187
190
+ ` update ` : the function that gets executed when ever a "_ signal_ "
188
191
is received from the client (_ person using the app_ ).
189
192
+ ` view ` : the function that renders the DOM (_ see: section 5.3 below_ )
@@ -247,7 +250,7 @@ or you want _more_ detail/examples,
247
250
#### 5.1.1 ` mount ` > render initial view
248
251
249
252
The last line in the ` mount ` function is to _ render_ the ` view ` function
250
- for the first time passing in the ` signal ` function, initial state
253
+ for the first time passing in the ` signal ` function, initial model (" state")
251
254
and root element. This is the _ initial_ rendering of the UI.
252
255
253
256
@@ -283,11 +286,11 @@ to the required function for processing.
283
286
284
287
In the case of our simple counter we aren't defining functions for each ` case ` :
285
288
``` js
286
- function update (model , action ) { // Update function takes the current state
289
+ function update (model , action ) { // Update function takes the current model
287
290
switch (action) { // and an action (String) runs a switch
288
291
case Inc: return model + 1 ; // add 1 to the model
289
292
case Dec: return model - 1 ; // subtract 1 from model
290
- default : return model; // if no action, return curent state .
293
+ default : return model; // if no action, return current model .
291
294
} // (default action always returns current)
292
295
}
293
296
```
@@ -333,7 +336,7 @@ function view(signal, model, root) {
333
336
The ` view ` receives three arguments:
334
337
+ ` signal ` defined above in ` mount ` (_ above_ ) tells each (DOM) element
335
338
how to to "handle" the user input.
336
- + ` model ` a reference to the current state of the application .
339
+ + ` model ` a reference to the _ current _ value of the counter .
337
340
+ ` root ` a reference to the root DOM element where the app is _ mounted_ .
338
341
339
342
The ` view ` function starts by _ emptying_
@@ -636,7 +639,7 @@ test('reset button should be present on page', function(assert) {
636
639
assert.equal(reset.length, 1);
637
640
});
638
641
639
- test('Click reset button resets state to 0', function(assert) {
642
+ test('Click reset button resets model (counter) to 0', function(assert) {
640
643
mount({model: 7, update: update, view: view}, id); // set initial state
641
644
var root = document.getElementById(id);
642
645
assert.equal(root.getElementsByClassName('count')[0].textContent, 7);
0 commit comments