lace:model
Bind a form input to a component property with two-way sync. The property is the source of truth: change it on the server and the input re-renders; edit the input and the property updates.
Syntax
class Search(Component):
query: str = ""
<input lace:model="query">
<p>Searching for: {{ query }}</p>
By default binding is deferred - the value syncs on the next action, not on every keystroke, saving a round-trip per character. Modifiers change when a sync happens:
<input lace:model.live="q"> {# every keystroke #}
<input lace:model.live.debounce.300ms="q"> {# debounced (default 300ms) #}
<input lace:model.blur="email"> {# on focus loss - good for validation #}
<input lace:model.change="choice"> {# on change - selects, checkboxes, radios #}
echo: (stays empty until you press Send)
echo:
echo:
echo:
class ModelModifiers(Component):
deferred: str = ""
live: str = ""
debounced: str = ""
on_blur: str = ""
def sync(self):
# No-op: clicking just opens a request - the deferred value rides along with it.
pass
def inline_template(self):
return """
<div class="...">
<div>
<label>default - deferred, syncs on the next action</label>
<input lace:model="deferred">
<button lace:click="sync">Send</button>
<p>echo: {{ deferred }} <span>(stays empty until you press Send)</span></p>
</div>
<div>
<label>.live - every keystroke</label>
<input lace:model.live="live">
<p>echo: {{ live }}</p>
</div>
<div>
<label>.live.debounce.500ms - after a pause</label>
<input lace:model.live.debounce.500ms="debounced">
<p>echo: {{ debounced }}</p>
</div>
<div>
<label>.blur - when the field loses focus</label>
<input lace:model.blur="on_blur">
<p>echo: {{ on_blur }}</p>
</div>
</div>
"""
Modifiers
| Modifier | Syncs | Use for |
|---|---|---|
| (none) | on the next action | forms - one round-trip on submit |
.live |
every keystroke | live search / preview (add .debounce.Nms to throttle) |
.blur |
when the field loses focus | per-field validation |
.change |
on the change event | selects, checkboxes, radios |
Nested properties
Dotted paths bind into nested data - dict keys, or fields of a bound model:
<input lace:model="settings.notifications">
<input lace:model="user.email">
Binding to a Django model field (like
user.email) requires that field on the component's binding allowlist - see Working with Models. Plain properties and dict keys need no allowlist.
Reacting to changes
Define an updated_<property> method to run code whenever a property changes:
class MyComponent(Component):
search_query: str = ""
def updated_search_query(self, value):
self.results = search_database(value)