liveBook · Manning

Data binding

As explained in Vanilla Web

Dynamic data binding synchronizes changing application data with user-interface elements, potentially in both directions.

Data binding keeps application state and the user interface in sync. A render function writes state into the DOM; input, change, or click events write user actions back into state. This two-way flow does not require automatic synchronization or a framework runtime: both directions can be explicit. section 13.2 section 13.2.1

const state = {
  count: 0,
};

function render() {
  count.value = state.count;
}

increment.addEventListener("click", () => {
  state.count += 1;
  render();
});

decrement.addEventListener("click", () => {
  state.count -= 1;
  render();
});

render();

The counter shows the smallest binding: button events change state.count, then render() updates the output. Calling render() once also displays the initial value. section 13.2.1

Start with direct DOM assignments when a small interface needs only a few bindings. Other choices include data-* bindings, template strings, <template> cloning, EventTarget stores, Proxy wrappers, and signals. Each trades some explicitness for ergonomics; a render function, optionally scheduled with queueMicrotask() to batch updates, is enough for many interfaces. Add a store or other abstraction when the state-sharing or update pattern warrants it. section 13.2 section 13.5

For production code, query elements with document.getElementById() or from a known root rather than relying on element IDs becoming global variables. Keep derived values computed from source state instead of maintaining duplicate state. section 13.2.1 section 13.5

How Vanilla Web covers it

How deep it goes
Works through an explicit counter binding and surveys progressively more abstract techniques without requiring a framework runtime.
Examples
Uses a counter whose buttons update state and whose render function updates an output element.
What it assumes
Assumes familiarity with JavaScript objects, DOM elements, and event listeners while building binding from those primitives.
What you can do after
Build it from scratch with a state object, render function, and event listeners; choose more elaborate bindings when needed.
How it connects
Connects application state, DOM rendering, browser events, derived data, and reactive update scheduling.

Also explained in

Compare how 7 books explain data binding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.