HTML bindings
An HTML binding is a data-bind* attribute on a view element that pagelove.mjs uses to populate that element from a schema instance — and, when the element is editable, to write user edits back into the schema.
When to reach for it
Reach for a binding whenever a view element needs to show a value from a schema article, or whenever a user edit on the view needs to flow back into the record. Bindings are how the rendered light-DOM view of an instance stays in sync with the underlying <article> without any imperative wiring.
The three binding attributes
| Attribute | Direction | What it does |
|---|---|---|
data-bind="prop" |
two-way (when the element is editable) | The element's text content (or value, on form controls) reflects the named property. User edits are written back to the schema article. |
data-bind-attr="prop1 prop2" |
two-way when observed | Forwards properties to data-* attributes on the view root. The runtime also observes declared attributes within its view and writes changes back to the linked schema article. |
data-bind-<htmlattr>="prop" |
read-only | Binds one specific HTML attribute to a property value. data-bind-href="url" sets href, data-bind-src="photo" sets src, data-bind-datetime="publishedAt" sets datetime. |
Property lookup is always scoped: pagelove.mjs resolves each prop against the direct [itemprop="<prop>"] children of the current schema scope, not against descendants.
Two-way updates
A data-bind element participates in writeback when user input reaches it through one of the delegated DOM events handled by pagelove.mjs:
| Event | Target selector | Purpose |
|---|---|---|
change |
nearest [data-bind] |
Commits only when the binding mode is immediate; this is the default for select, checkbox, date and color controls. |
focusout |
[contenteditable][data-bind], input[data-bind], select[data-bind], textarea[data-bind] |
Commits only in blur mode; this is the default for text inputs, textarea and contenteditable="true". |
input |
nearest [data-bind] |
Schedules a debounced commit only when data-commit="idle:<milliseconds>" is configured. |
The mode can be overridden with data-commit="blur", data-commit="immediate" or data-commit="idle:500". The inspected runtime defaults editable text to blur; merely adding data-bind does not enable live debounced commits. Debounce timers are keyed by the closest data-for identity and property, so two controls for the same pair share a timer. Do not assume blur flushes an idle-mode timer, or that changing the DOM creates a frozen pending submission.
Checkbox bindings read .checked as the strings 'true' or 'false' and set .checked from whether the schema value is exactly 'true'. Other inputs, select and textarea use .value; contenteditable="true" reads trimmed innerText, and other elements read trimmed textContent. The select path reads one .value, not a collection of selected options; radios have no checked-state specialization. Verify adapters for multi-select, radio groups and other control types before relying on them.
The default schema writer updates a meta element's content, a time element's datetime, or another property's textContent. It compares values to avoid redundant schema mutations, but the commit path can still issue a persistence request for an unchanged value. Updating a form control's current property is not the same as updating its serialized HTML defaults; do not assume outerHTML preserves current values, checked state or selections. Keep temporary UI outside persisted source and verify the actual submitted HTML and read-back.
For data-bind-attr, the observer collects attribute names from discovered template roots and watches matching changes under the configured view. Writeback requires the changed element to declare the property in data-bind-attr and carry a data-for article identity. That path calls afterCommit before batched persistence and does not call beforeCommit; the read-only data-bind-<htmlattr> family is a separate mechanism. Neither local schema updates nor hooks confirm a saved server result. See commit and persistence boundaries.
Repetition
A data-bind element becomes a repeating stamp when any of these three conditions holds:
- The view element carries
itemscope, even when there are no matching property children. - The schema scope contains more than one direct child matching
[itemprop="<prop>"]. - The discovered definition for this instance's schema declares the property with a cardinality ending in
..n, such as0..nor1..n. This condition applies with zero or one matching child too; it requires that the property definition has been discovered.
#repeatElement() removes the original stamp and leaves a comment marker of the form <!--data-bind:<prop>-->. It inserts one clone per matching schema child after that marker. With no values there are no clones, so an empty list field disappears. With one value it still uses a clone. If none of the three conditions holds, the singular binding remains in place; when there is no source value, that pass leaves the existing element unchanged. Put any empty-list prompt or add control outside the repeating stamp if it must remain available.
For a scalar schema child, the clone is populated through the shared view writer. For example, it sets a text input's value, a checkbox's checked state, or a time element's datetime; ordinary display elements receive text. It does not universally write textContent. A time clone with existing text keeps that text when no transform is supplied. If the schema child has itemscope, the clone receives data-for="<schemaId>" when the child has an id, and #populateBindings() recurses against that nested schema scope.
These are local rendering rules. Repetition does not establish server validation, permission or persistence, and the nested-scope ownership rules below still apply.
Nested scopes
A data-bind that sits inside a deeper [data-for] element is owned by that inner scope, not by the outer pass. During the outer viewRoot.querySelectorAll('[data-bind]') iteration, any element whose closest [data-for] ancestor is not the current viewRoot is skipped. The same rule applies to the data-bind-<htmlattr> sweep. This is what allows nested components to manage their own bindings without the parent rewriting them on every pass.
Examples
Two-way text binding on a heading
<template itemtype="https://schema.host/Note">
<article class="note">
<h1 data-bind="title" contenteditable></h1>
<p data-bind="body" contenteditable></p>
</article>
</template>
The heading and paragraph reflect the instance's title and body. With this markup, editing commits to the local schema article on focusout, then attempts persistence. Debounced input requires an explicit data-commit="idle:<milliseconds>"; neither event is itself confirmation that the server saved the edit.
Forwarding properties as data-* attributes
<template itemtype="https://schema.host/StickyNote">
<article class="sticky" data-bind-attr="color x y">
<h1 data-bind="title" contenteditable></h1>
</article>
</template>
The root <article> receives data-color, data-x, and data-y reflecting the matching properties on the instance. CSS can then read them:
.sticky { background: attr(data-color); transform: translate(attr(data-x px), attr(data-y px)); }
Binding an HTML attribute
<template itemtype="https://schema.host/Link">
<article>
<a data-bind-href="url" data-bind="label"></a>
<time data-bind-datetime="publishedAt" data-bind="publishedAt"></time>
</article>
</template>
The <a> gets its href from url and its visible text from label. The <time> gets its datetime attribute from publishedAt and its visible text from the same property.
See also
- Schema instances in HTML — the
<article>markup a binding reads from and writes to. - Schema definitions in HTML — how the properties named by a binding are declared.
- The Pagelove class — the runtime that discovers templates and wires up delegated event handlers.