Spanish Total.js
English
Documentation For AI agents
Contents

Data binding

Connect HTML elements to paths with ui-bind and data-bind, without writing JavaScript.

.md GitHub
On this page

Data binding connects an HTML element to a path. When the value changes, the element updates by itself. You describe what should happen with short commands, and most pages need no extra JavaScript for it.

The ui-bind element

<ui-bind> takes the path in the path attribute and a list of commands in the config attribute. Commands are separated by a semicolon, and each one has the form command:value.

<p><ui-bind path="user.name" config="text"></ui-bind></p>
<p><ui-bind path="user.name" config="text:value.toUpperCase()"></ui-bind></p>
<p><ui-bind path="user.name" config="text:(value, path) => path + ' = ' + value"></ui-bind></p>
<p><ui-bind path="user.name" config="text:shout"></ui-bind></p>

<script>
    function shout(value) {
        return value + '!';
    }

    SET('user.name', 'Ana');
</script>

The four paragraphs show Ana, ANA, user.name = Ana and Ana!. The part after the colon can take these forms:

FormExampleResult
NothingtextThe value itself.
Expressiontext:value.toUpperCase()The result of the expression.
Arrow functiontext:(value, path) => path + valueThe return value. It also receives the element as the third argument.
Function nametext:shoutThe return value of the global function shout(value, path, element).

An expression must contain the word value. Text without it is read as the path of a function, so a fixed string such as text:'Hello' is not valid.

Bind any element

The data-bind and bind attributes add a binding to an existing element. The path and the commands are separated by two underscores.

<div id="a" data-bind="user.name__text"></div>
<div id="b" bind="user.name__text:value.length"></div>
<div id="c" data-bind="user.name__text__title:'Name: ' + value__.long:value.length > 2"></div>

<script>
    SET('user.name', 'Ana');
</script>

Show and hide elements

CommandWhen the result is trueCSS class it uses
showThe element is displayed, otherwise it is hidden.hidden
hideThe element is hidden.hidden
visibleThe element is visible, otherwise it keeps its space but cannot be seen.invisible
invisibleThe element keeps its space but cannot be seen.invisible

A command that starts with a dot toggles that CSS class.

<ui-bind id="a" path="cart.count" config="show:value > 0;text:value + ' items'"></ui-bind>
<ui-bind id="b" path="cart.count" config="hide:value > 0">Your cart is empty</ui-bind>
<ui-bind id="c" path="cart.count" config="visible:value > 0">Checkout</ui-bind>
<ui-bind id="d" path="cart.count" config=".full:value >= 3;.empty:value === 0">Status</ui-bind>

<script>
    SET('cart.count', 0);
</script>

With a count of 0, the first element is hidden, the second shows its text, the third is invisible and the fourth has the class empty. With a count of 3, the first shows 3 items and the fourth has the class full.

Update child elements

Add a CSS selector after the command to apply it to the elements inside the binding.

<ui-bind id="card" path="user" config="text b:value.name;html em:value.bio;.admin i:value.admin;attr data-id:value.id">
    <b></b> <em></em> <i>Administrator</i>
</ui-bind>

<script>
    SET('user', { id: 7, name: 'Ana', bio: '<u>Engineer</u>', admin: true });
</script>

The result is <b>Ana</b> <em><u>Engineer</u></em> <i class="admin">Administrator</i>, and the <ui-bind> element receives data-id="7".

Content, attributes and form controls

CommandEffect
textSets the text.
htmlSets the HTML.
valueSets the value of a form control.
checkedChecks or unchecks a checkbox.
disabledDisables the element when the result is true.
enabledEnables the element when the result is true.
titleSets the title attribute.
hrefSets the href attribute.
srcSets the src attribute.
attr nameSets the attribute name.
<a id="a" data-bind="link__href:value.url__title:value.name__text:value.name"></a>
<img id="b" data-bind="link.icon__src__attr alt:value" />
<input id="c" data-bind="link.name__value" />
<input id="d" type="checkbox" data-bind="link.active__checked" />
<button id="e" data-bind="link.active__disabled:!value">Save</button>
<button id="f" data-bind="link.active__enabled">Send</button>

<script>
    SET('link', { url: '/docs/', name: 'Docs', icon: '/icon.png', active: false });
</script>

On a <ui-bind> element, disabled and enabled apply to the components, inputs, text areas, select boxes and buttons inside it.

Render a template

The template command renders a Tangular template placed in a <script type="text/html"> or <template> element inside the binding. The bound value is available as value.

<ui-bind id="list" path="todo" config="template">
    <script type="text/html">
        <ul>
        {{ foreach item in value }}
            <li>{{ item.name }}</li>
        {{ end }}
        </ul>
    </script>
</ui-bind>

<script>
    SET('todo', [{ name: 'Write' }, { name: 'Test' }]);
</script>

React to clicks and changes

click calls a function with the element, the event, the current value and the path. exec calls a function each time the value changes.

<button id="save" data-bind="order.id__click:save">Save</button>
<ui-bind path="order.total" config="exec:ontotal"></ui-bind>

<script>
    function save(el, e, value, path) {
        console.log('click', el.attr('id'), e.type, value, path);
    }

    function ontotal(value, path, el) {
        console.log('total', value, path);
    }

    SET('order', { id: 100, total: 50 });
</script>

Format the output

CommandEffect
format:2Formats a number with that many decimals.
format:yyyy-MM-ddFormats a date.
emptyShows --- for an empty value.
empty:textShows text for an empty value.
default:expressionUses the result of the expression when the value is null or undefined.

A ! in front of a command skips it while the value is null or undefined, so the original content of the element stays visible.

<p id="a"><ui-bind path="p.price" config="text;format:2"></ui-bind></p>
<p id="b"><ui-bind path="p.date" config="text;format:yyyy-MM-dd"></ui-bind></p>
<p id="c"><ui-bind path="p.note" config="text;empty"></ui-bind></p>
<p id="d"><ui-bind path="p.note" config="text;empty:Nothing yet"></ui-bind></p>
<p id="e"><ui-bind path="p.note" config="text;default:'Not set'"></ui-bind></p>
<p id="f"><ui-bind path="p.note" config="!text">Loading</ui-bind></p>

<script>
    SET('p', { price: 1234.5, date: new Date(2026, 0, 15), note: null });
</script>

The paragraphs show 1 234.50, 2026-01-15, ---, Nothing yet, Not set and Loading.

Control when a binding updates

CommandEffect
track:a,bOn a binding to an object, reacts only to changes of the listed fields.
strictIgnores changes made to paths below the bound path.
onceRuns one time only.
changesRuns only when the value differs from the previous one.
delay:300Waits this many milliseconds.
<ui-bind id="a" path="user" config="text:value.name;track:name"></ui-bind>
<ui-bind id="b" path="user" config="text:value.name;strict"></ui-bind>
<ui-bind id="c" path="user.age" config="text;once"></ui-bind>
<ui-bind id="d" path="user.age" config="exec:onage;changes"></ui-bind>
<ui-bind id="e" path="user.age" config="text;delay:300"></ui-bind>

<script>
    function onage(value) {
        console.log('age changed', value);
    }

    SET('user', { name: 'Ana', age: 30 });
</script>

Update hidden elements

Commands that change the content are skipped while show, hide, visible or invisible keep the element hidden, and they run again when it appears. A ~ in front of a command runs it anyway.

<ui-bind id="a" path="box" config="show:value.open;text:value.title"></ui-bind>
<ui-bind id="b" path="box" config="show:value.open;~text:value.title"></ui-bind>

<script>
    SET('box', { open: false, title: 'Hello' });
</script>

While the box is closed, the first element is empty and the second already contains Hello.

Target another element

The element attribute applies the binding to any element selected with a CSS selector. The child attribute applies it to an element inside the binding.

<ui-bind path="user.name" config="text" element="#target"></ui-bind>
<ui-bind id="wrap" path="user.name" config="text" child="b"><i>Name:</i> <b></b></ui-bind>
<span id="target"></span>

<script>
    SET('user.name', 'Ana');
</script>

Pass values to a component

set sends a value to a component inside the binding, and config reconfigures it. Both are useful for a component that has no path of its own.

<ui-bind path="settings.label" config="set:value.toUpperCase();config:'color:' + value">
    <ui-component name="badge"></ui-component>
</ui-bind>

<script>
    COMPONENT('badge', function(self, config) {
        self.setter = function(value) {
            self.text(value + ' (' + config.color + ')');
        };
    });

    SET('settings.label', 'red');
</script>

The component renders RED (red).

Two-way binding

Bindings only write to the page. To send user input back to the model, use a component with an input marked with data-jc-bind. Typing Ben in this box changes profile.name, and the paragraph follows.

<ui-component name="textbox" path="profile.name"></ui-component>
<p>Hello <ui-bind path="profile.name" config="text"></ui-bind></p>

<script>
    COMPONENT('textbox', function(self) {
        self.make = function() {
            self.html('<input type="text" data-jc-bind="" />');
        };
    });

    SET('profile.name', 'Ana');
</script>

Creating components describes how inputs work inside components.

Made in Europe