Domma Reactive User Manual

Components and params

Updated by Darryl Waterhouse on 17 October 2026 5 min read

A component is a piece of markup with its own private state and its own teardown. Register one, then render it anywhere:

import {registerComponent, observable, computed} from 'domma-reactive';

registerComponent('contact-card', {
    template: `
        <div class="card">
            <b data-bind-text="contact.name.value"></b>
            <button data-on-click="toggle" data-bind-text="label.value"></button>
            <input data-if="editing.value" data-model="contact.name.value">
        </div>`,

    create(params) {
        const editing = observable(false);

        return {
            contact: params.contact,
            editing,
            label: computed(() => (editing.value ? 'done' : 'edit')),
            toggle() { editing.value = !editing.value; }
        };
    }
});
<div data-component="'contact-card'" data-param-contact="$data"></div>

editing belongs to the card. Ten cards on a page have ten of them, and none can see the others - which is the whole reason to reach for a component rather than a {{#each}} body with an editingId held on the list.

The name is an expression, like every binding value here, which is why a literal one takes inner quotes. That costs a pair of quotes in the common case and buys dynamic components for nothing.

Two definition shapes

create is a plain factory - no new, no constructor form, no second createViewModel spelling. It receives the params and {element}, and returns the object the template resolves against.

Leave it out and the component is template-only, with the params themselves as $data:

registerComponent('badge', {template: '<b class="badge" data-bind-text="label"></b>'});
<span data-component="'badge'" data-param-label="status"></span>

That keeps the trivial case trivial, which is where Knockout's viewModel-or-constructor ambiguity does the most damage.

Params, in two spellings

<!-- one param named in the attribute - the common case -->
<div data-component="'contact-card'"
     data-param-contact="$data"
     data-param-editable="canEdit"></div>

<!-- an object the view model already holds - many params at once -->
<div data-component="'contact-card'" data-params="cardParams"></div>

This is the pair data-bind-style established and data-options-* followed, and it exists for the same reason: Knockout spells params as an object literal. data-params="{a: b}" parses since 1.2, but one param named in an attribute is the common case, and it is the one an object literal makes awkward.

Both forms may appear together. They merge, and a named attribute beats the same key in the object - the more specific spelling wins. A collision warns once, because it is almost always a mistake.

Attribute names are kebab-case and arrive camelCased: data-param-first-name is params.firstName. An HTML attribute name is lowercased by the parser, so data-param-firstName could not survive the round trip.

data-param-* on an element with no data-component warns at compile time. Nothing would ever read it, and the symptom - a component whose every param is undefined - is indistinguishable from a typo in the component name.

Params pass by reference, and the markup says which

Nothing decides this. It falls out of reads being explicit:

Markup The view model receives Can it write back?
data-param-contact="user.name" the observable itself yes - the parent sees the write
data-param-contact="user.name.value" a snapshot of the value no

Knockout needs a documented convention here and its users still get it wrong, because params: {a: x} and params: {a: x()} look equally plausible at a glance. Here they are different expressions, and the difference is the same .value you already read through.

Params are evaluated once, when the instance is created - a constructor argument, not a live binding. Observables stay live because they are references; plain expressions are snapshots. The params object is frozen, because it is an input rather than scratch space; the observables inside it stay writable through .value, which is the intended path back to the parent.

$component

At the top of a component's template, $data is the view model, so a template reads its own state unqualified. Inside a {{#each}} it is not - $data is the row. $component still reaches the view model:

<!-- inside a component's own template -->
{{#each rows key=id}}
    <li>
        {{name}}
        <button data-on-click="$component.select">select</button>
    </li>
{{/each}}

It is inherited by child contexts exactly as $root is, so it keeps answering however deep the nesting goes - which is the only reason the name exists. It is null outside a component.

A component is a boundary for $data and nothing else: $parents and $parentContext see straight through it, so a component rendered inside a list can still reach that list's data and its $index.

Swapping a component

Point data-component at an observable and the rendered component follows it:

<div data-component="currentView.value"></div>
currentView.value = 'settings-panel';   // the editor is disposed, the panel is built

This is how a great many Knockout applications route. Changing the name tears the old instance down - view model dispose() first, then its effects, then its nodes - and builds the replacement, re-evaluating every param against the context in force at that moment. Setting the name to what it already is does nothing at all, so an unrelated update never costs you a half-typed input.

The host element, and what happens to its children

A component renders inside its element rather than replacing it, as Knockout's component: does. The host keeps its own attributes, classes and identity across a swap, and create(params, {element}) receives it.

Its original children are not discarded: they are the component's slot content.

Disposal

If the view model has a dispose(), it runs on teardown, before the instance's effects are destroyed:

create(params) {
    const timer = setInterval(tick, 1000);
    return {
        dispose() { clearInterval(timer); }
    };
}

There are no other lifecycle hooks. dispose() is the same contract handle.dispose() and controller.destroy() already offer, and adding more would be inventing the framework this package is not.

Teardown happens when the name changes, when an enclosing region re-renders over the host, when the host's key leaves a list, or when the controller is destroyed. A dispose() that throws is warned about and stepped over - one component failing to clean up must not stop the next one from cleaning up.

Failure is never fatal

As everywhere else: one warning naming the expression and the template, and that component alone is skipped.

Situation Behaviour
The name expression does not parse warn once, host left empty
The name is not a string warn once naming the quoting rule, host left empty
No component registered under that name warn once naming the name, host left empty
A param expression does not parse warn once, that param is absent from the object
create() throws warn once, host left empty, no instance registered
dispose() throws warn, and teardown continues

registerComponent is the exception: it throws on a bad definition, as registerExtender does. A bad registration is a programming error at startup, where a bad expression is authored data met halfway through a paint.