Binding Fields to DOM
On the previous page, we saw the most generic way fields relate to the DOM: a component owns fields, and those fields are reflected in the DOM so that state remains visible and inspectable.
In its simplest form, a field may exist only as component state, or be defined
via
data-*
attributes on the component's root element and that's often enough.
However, many components need something more specific:
- A field rendered as visible text inside the component.
- A field stored in an attribute of a particular element.
-
A field mapped to a native HTML attribute such as
titleorvalue
Qite supports these patterns explicitly and in a structured way. This page explains the two supported mechanisms.
Binding fields to element.textContent
Sometimes a field represents text that should appear inside the component.
For example, a label, a hint, a counter, or an error message. Instead of
manually updating
textContent, you can bind the field to an element
using
data-field
:
<div data-component="TextInput">
<label data-field="label"></label>
</div>
When the component sets:
this.fields.set("label", "Delivery address, 123");
the
<label>
element's
textContent
updates automatically.
Because it keeps rendering declarative. The component sets the field, and the DOM reflects it. There is no manual DOM manipulation scattered throughout your code.
Binding fields to attributes with data-field-map
Sometimes a field should not be rendered as visible text, but stored
in an attribute of a specific element. For this, Qite uses
data-field-map
The explicit syntax is:
field_name:attribute_name
For example:
<div data-component="Profile">
<input data-field-map="tooltip:title" title=""/>
</div>
Here, the field
tooltip
is read from and written to the native
title
attribute. When you call:
this.fields.set("tooltip", "Click to edit");
Qite updates the
title
attribute of the
<input>
element. Or, if you omit the
attribute name:
<input data-field-map="placeholder" data-placeholder="">
the mapping defaults to
placeholder:data-placeholder. Note how underscores in
the field name become dashes when mapped to HTML attribute names.
Because some values belong in attributes rather than text.
placeholder,
title,
value, and many ARIA attributes must be attributes
to work correctly. So
data-field-map
lets you keep those values as fields while
respecting how HTML actually works.
Sometimes you don't to write field map for every component HTML, so you can set default field maps in JavaScript code of the component like this:
export default class ButtonComponent extends BaseComponent {
static default_field_map = "lockable";
...
This is take directly from the button_component.js shipped with Qite.js. Now, you
can simply say
<button data-lockable="true">
in your HTML without also
adding
data-fiel-map
attribute.
Note that this default map only applies to the component root element. So this won't work, for example:
<div data-component="Switch">
<button data-status="off"></button>
</div>
You'll have to use
data-field-map
on that child element:
<div data-component="Switch">
<button data-field-map="status" data-status="off"></button>
</div>
Choosing the right mechanism
Use:
-
data-fieldwhen the field represents visible text. -
data-field-mapwhen the field must live in an attribute.
Both mechanisms are declarative and in both cases — you set the field on the component, and Qite ensures the DOM stays in sync.