Input Binding
The Duck way of input binding.
Demo
live demo | files
Interactive Demo
Enter a value...
Recipe
function Store(data) {
  const events = {};

  let value = data.value;

  function getValue() { return value; }
  function setValue(val) { value = val; emit('value'); }

  return {
    getValue,
    setValue,
    on, off
  };

  // ...
}
JavaScript
function Input(data) {
  let elem;
  const store = data.store;

  function construct() {
    elem = document.createElement('input');
    bind();
    update();
  }

  function update() {
    elem.value = store.getValue();
  }

  function enter() {}
  function exit() {}

  function destroy() {
    unbind();
  }

  function onInput() {
    store.setValue(elem.value);
  }

  function bind() {
    elem.addEventListener('input', onInput);
    store.on('value', update);
  }
  function unbind() {
    elem.removeEventListener('input', onInput);
    store.off('value', update);
  }

  construct();
  return { elem, data, update, enter, exit, destroy };
}
JavaScript
function App(data) {
  let elem;
  const store = data.store;

  let textElem;
  let input;

  function construct() {
    elem = document.createElement('div');
    {
      textElem = document.createElement('div');
      elem.appendChild(textElem);
      input = new Input({ store });
      elem.appendChild(input.elem);
    }
    bind();
    update();
  }

  function update() {
    textElem.innerHTML = store.getValue() || 'Enter a value...';
  }

  function enter() {}
  function exit() {}

  function destroy() {
    input.destroy();
    unbind();
  }

  function bind() {
    store.on('value', update);
  }
  function unbind() {
    store.off('value', update);
  }

  construct();
  return { elem, data, update, enter, exit, destroy };
}
JavaScript
Walkthrough
To perform input binding in Duck, use a store and a component. The store tracks the state of the input, while the component simply wraps a traditional input element. When text is entered into the input component, you update the store. When the store value is updated, you update the input component.
The App component here is used as a container to bring it all together.