Domma Reactive User Manual

Activate existing HTML

Updated by Darryl Waterhouse on 17 October 2026 2 min read

The other direction from compile(). Point it at HTML that already exists - server-rendered, hand-written, whatever - and it activates the binding attributes in place, leaving the markup otherwise as it found it. No build step, no second source of truth for the markup.

<div id="app">
    <h1 data-bind-text="title">Rendered by the server</h1>
    <button data-on-click="save">Save</button>
    <input data-model="query.value" value="rendered by the server">
    <p data-if="showHelp">Help text.</p>
    <ul data-each="rows.value key=id">
        <li data-bind-text="name">template row</li>
    </ul>
</div>
import {applyBindings, observable, observableArray} from 'domma-reactive';

const handle = applyBindings({
    title: 'Live',
    query: observable(''),
    showHelp: false,
    rows: observableArray([{id: 1, name: 'Ada'}]),
    save() { /* … */ }
}, document.querySelector('#app'));

Every binding gets its own effect, so a view model built from observables updates itself. For a plain, untracked object, handle.update(data) re-runs everything.

Note data-model="query.value", not data-model="query" - query holds an observable, and the no-unwrapping rule applies in a binding exactly as it does in JavaScript. Binding the bare name warns ("An observable is not its value"), leaves the input empty, and would replace the observable on the first keystroke.

Returns {bindings, context(), update(data), dispose()}
Idempotent applying twice skips elements already bound and warns once, naming the root
Disposable dispose() drops every effect, listener, list instance and marker it created, restores a hidden data-if element, and leaves the markup as it was found

{{ }} in already-rendered DOM is not interpolated

Deliberately, and it says so once if it finds a token that looks like a binding.

There is nothing coherent to do with it. Either the server rendered the value - in which case the token is gone and there is only text that happens to say "Ada" - or the server emitted the raw token, in which case the page was broken until JavaScript ran, which is the thing server rendering exists to avoid. Guessing which text nodes are dynamic is not possible, and rewriting every text node into anchored spans would mutate, destructively, the markup this function promises to leave alone.

data-bind-text="expr" is the supported spelling. It is explicit, greppable, and the server can render the text and the attribute together.

The one exception is the contents of a data-each, which are a template rather than rendered output: they are lifted out of the document, compiled and cloned per item, so mustache works there because there it means something.

data-if here detaches; in a template it re-renders

applyBindings implements data-if by removing the element and putting the same node back, so it keeps its children, its listeners and its focus across a toggle. compile() cannot do that - while an element is detached, the bindings inside it are invisible to re-indexing, so it would come back stale - and re-renders its region instead. This is the one place the two entry points differ in behaviour rather than in input.

A custom binding declaring region: true is refused by applyBindings, with an explanation: a region handler re-renders from a captured template body, and here the markup is the page.