Refactoring - update method.
h3rald h3rald@h3rald.com
Mon, 13 Apr 2020 22:19:52 +0200
5 files changed,
8 insertions(+),
19 deletions(-)
M
example/assets/js/components/addTodoForm.js
→
example/assets/js/components/addTodoForm.js
@@ -5,7 +5,7 @@ const addTodo = () => {
const newTodo = document.getElementById("new-todo"); if (!newTodo.value) { h3.dispatch("error/set"); - h3.dispatch("$update"); + h3.update() document.getElementById("new-todo").focus(); return; }@@ -15,7 +15,7 @@ key: `todo_${Date.now()}__${newTodo.value}`, // Make todos "unique-enough" to ensure they are processed correctly
text: newTodo.value, }); newTodo.value = ""; - h3.dispatch("$update"); + h3.update() document.getElementById("new-todo").focus(); }; const addTodoOnEnter = (event) => {
M
example/assets/js/components/paginator.js
→
example/assets/js/components/paginator.js
@@ -18,14 +18,14 @@ const page = h3.state('page');
const newPage = page - 1; h3.dispatch("pages/set", newPage); window.location.hash = `/?page=${newPage}`; - h3.dispatch("$update"); + h3.update() } function setNextPage() { const page = h3.state('page'); const newPage = page + 1; h3.dispatch("pages/set", newPage); window.location.hash = `/?page=${newPage}`; - h3.dispatch("$update"); + h3.update() } return h3("div.paginator", [ h3(
M
example/assets/js/components/todo.js
→
example/assets/js/components/todo.js
@@ -4,11 +4,11 @@ export default function Todo(data) {
const todoStateClass = data.done ? ".done" : ".todo"; const toggleTodo = (todo) => { h3.dispatch("todos/toggle", data); - h3.dispatch("$update"); + h3.update() }; const removeTodo = (todo) => { h3.dispatch("todos/remove", data); - h3.dispatch("$update"); + h3.update() }; return h3(`div#${data.key}.todo-item`, [ h3(`div.todo-content${todoStateClass}`, [
M
h3.js
→
h3.js
@@ -292,7 +292,7 @@ this.state = {};
} dispatch(event, data) { if (this.events[event]) { - if (event !== "log") this.dispatch("log", { event, data }); + if (event !== "$log") this.dispatch("$log", { event, data }); let changes = {}; let changed; this.events[event].forEach((i) => {@@ -313,16 +313,6 @@ this.events[event] = this.events[event].filter((i) => i !== cb);
}; } } - -const createStore = (modules) => { - const store = new Store(); - - modules.forEach((i) => { - if (i) i(store); - }); - - return store; -}; class Route { constructor({ path, def, query, parts, fallback }) {@@ -439,7 +429,6 @@ // Initialize router
//router = new Router({element, routes}) // Render //router.start(); - store.on('$update', updateFn); element.appendChild(vnode.render()); }