DOM Bindings
VaneJS uses HTML data attributes to create reactive bindings between your state and the DOM.
Data Binding
data-bind
The data-bind
attribute creates a one-way binding between a state value and an element's content.
html
<p data-bind="user.name"></p>
When the state changes, the element's content updates automatically:
javascript
$setState('user', { name: 'John' });
// The <p> element will now show "John"
$updateState('user', { name: 'Jane' });
// The <p> element automatically updates to show "Jane"
List Rendering
data-repeat
The data-repeat
attribute allows you to render lists of items based on array state.
html
<div data-repeat="user.skills in skill">
<template>
<div>
<h3 data-ritem="{skill}.label"></h3>
<div data-repeat="{skill}.tags in tag">
<template>
<span data-ritem="{tag}"></span>
</template>
</div>
</div>
</template>
</div>
State example:
javascript
$setState('user', {
skills: [
{ label: 'Web Dev', tags: ['html', 'css'] },
{ label: 'Backend', tags: ['node', 'python'] }
]
});
Template Requirements
- List items must be wrapped in a
<template>
tag - Use
data-ritem
to bind to individual item properties - Nested repeats are supported using the parent item's pointer
Conditional Rendering
data-if
The data-if
attribute allows you to conditionally render elements based on state values.
html
<div data-if="appState === 'loading'">
<template>
<p>Loading...</p>
</template>
</div>
javascript
$setState('appState', 'loading');
// Element will be visible
$setState('appState', 'loaded');
// Element will be removed from the DOM
Best Practices
- Always wrap repeated and conditional content in
<template>
tags - Use dot notation for accessing nested state properties
- Keep bindings simple and avoid complex expressions
- Use meaningful variable names in repeat expressions