Skip to content

Commit 3b8eb11

Browse files
committed
add render function to DRY code in elmish mount function for #44 / #55
1 parent d1bd85e commit 3b8eb11

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Diff for: examples/todo-list/elmish.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@ function empty (node) {
2020
* @param {Function} view function that renders HTML/DOM elements with model.
2121
* @param {String} root_element_id root DOM element in which the app is mounted
2222
*/
23-
function mount (model, update, view, root_element_id) {
23+
function mount (model, update, view, root_element_id, subscriptions) {
2424
var root = document.getElementById(root_element_id); // root DOM element
25-
function signal(action) { // signal function takes action
26-
return function callback() { // and returns callback
25+
26+
function render (mod, sig, root, subs) {
27+
localStorage.setItem('elmish_store', JSON.stringify(mod)); // save model!
28+
empty(root); // clear root element (container) before (re)rendering
29+
root.appendChild(view(mod, sig)) // render view based on model & signal
30+
if (subs && typeof subs === 'function') { subs(sig); } // event listeners
31+
}
32+
33+
function signal(action) { // signal function takes action
34+
// console.log('action:', action);
35+
return function callback() { // and returns callback
36+
model = JSON.parse(localStorage.getItem('elmish_store')) || model;
37+
// console.log('model BEFORE:', model);
2738
var updatedModel = update(action, model); // update model for the action
28-
localStorage.setItem('elmish_store', JSON.stringify(updatedModel));
29-
empty(root); // clear root el before rerender
30-
root.appendChild(view(updatedModel, signal)); // subsequent re-rendering
39+
// console.log('model AFTER:', updatedModel);
40+
render(updatedModel, signal, root, subscriptions);
3141
};
3242
};
3343
model = JSON.parse(localStorage.getItem('elmish_store')) || model;
34-
root.appendChild(view(model, signal)) // render initial model (once)
35-
localStorage.setItem('elmish_store', JSON.stringify(model)); // save model!
44+
render(model, signal, root, subscriptions);
3645
}
3746

3847
/**
@@ -51,7 +60,7 @@ function add_attributes (attrlist, node) {
5160
var a = attr.split('=');
5261
switch(a[0]) {
5362
case 'autofocus':
54-
node.autofocus = "";
63+
node.autofocus = "autofocus";
5564
node.focus();
5665
break;
5766
case 'checked':

0 commit comments

Comments
 (0)