dwyl / learn-elm-architecture-in-javascript

:unicorn: Learn how to build web apps using the Elm Architecture in "vanilla" JavaScript (step-by-step TDD tutorial)!
https://todomvc-app.herokuapp.com
GNU General Public License v2.0
213 stars 19 forks source link

BUG! Failing to supply the model as an argument to subscriptions ... #63

Closed nelsonic closed 6 years ago

nelsonic commented 6 years ago

at present the subscriptions in the todo list example (that I am busy writing... #45) do not receive the model as an argument:

/**
 * `subscriptions` let us "listen" for events such as "key press" or "click".
 * and respond according to a pre-defined update/action.
 * @param {Function} singal - the Elm Architicture "dispacher" which will run
 * both the "update" and "render" functions when invoked with singal(action)
 */
function subscriptions (signal) {
    var ENTER_KEY = 13; // add a new todo item when [Enter] key is pressed
    var ESCAPE_KEY = 27; // used for "escaping" when editing a Todo item

  document.addEventListener('keyup', function handler (e) {
    var new_todo = document.getElementById('new-todo');
    console.log('e.keyCode:', e.keyCode, '| key:', e.key);
    if (e.keyCode === ENTER_KEY && new_todo.value.length > 0) {
      signal('ADD')(); // invoke singal inner callback
      new_todo.value = ''; // reset <input> so we can add another todo
      document.getElementById('new-todo').focus();
    }
  });
}

This is an obvious fail (in retrospect) as we cannot "infer" the model when triggering an action! So my next step is to re-trace my own instructions and refactor the subscriptions function!

nelsonic commented 6 years ago

I figured it out without having to complicate the subscriptions! 🎉