Spanish Total.js
English
Documentation For AI agents
Contents

Data binding

How to render values of the model into the page with ui-bind and keep inputs and the model in sync.

.md GitHub
On this page

Data binding connects the model to the page. The <ui-bind> element renders a value from a path and renders it again whenever the value changes. Components add the opposite direction: they write what the user enters back to the model.

The ui-bind element

<ui-bind> has two main attributes. path is the value to listen to and config is a list of commands separated by semicolons. A command in the form name:expression uses the result of a JavaScript expression. Without an expression, the command uses the value itself.

<ui-bind path="user.name" config="text"></ui-bind>
<ui-bind path="user.name" config="text:value.toUpperCase()"></ui-bind>
<ui-bind path="user.age" config="text:value >= 18 ? 'adult' : 'minor'"></ui-bind>
SET('user', { name: 'Ana', age: 30 });

The three elements show Ana, ANA and adult.

An expression can use these variables:

VariableDescription
valueThe value at the path
element, elThe target element as a jQuery object
flagsThe flags of the operation
pathThe path that was changed

Inside config, write a semicolon as \;:

<ui-bind path="note.text" config="text:value + '\; ok';title:'Note\: ' + value"></ui-bind>
<ui-bind path="note.text" config="exec:FUNC.log"></ui-bind>
<ui-bind path="note.text" config="text:element.attr('data-prefix') + value" data-prefix="> "></ui-bind>

Commands

CommandEffect
textSets the text content
htmlSets the HTML content
valueSets the value of a form control
title, href, srcSets the attribute of the same name
attr nameSets the attribute name
.nameAdds the CSS class name when the result is truthy and removes it otherwise
showAdds the class hidden when the result is falsy
hideAdds the class hidden when the result is truthy
visibleAdds the class invisible when the result is falsy
invisibleAdds the class invisible when the result is truthy
disabledDisables the form controls inside the target when the result is truthy
enabledDisables the form controls inside the target when the result is falsy
checkedChecks the inputs inside the target when the result is truthy
templateRenders a Tangular template
execCalls a function with the arguments value, path and element

While show, hide, visible or invisible keeps the element hidden, text, html and template do not render. They render when the element is displayed again.

The ui-bind reference describes all commands and options.

Choosing the target

By default, a command changes the <ui-bind> element itself. Write a CSS selector after the command to change elements inside it:

<ui-bind path="product" config="text b:value.name;html span:'<i>' + value.price + ' EUR</i>';href a:value.url;title a:value.name;attr data-id a:value.id;.sale:value.discount > 0">
    <b></b>
    <span></span>
    <a>Detail</a>
</ui-bind>
SET('product', { id: 7, name: 'Pen', price: 12, url: '/products/pen/', discount: 10 });

The rendered result:

<ui-bind path="product" class="sale">
    <b>Pen</b>
    <span><i>12 EUR</i></span>
    <a href="/docs/products/pen/" title="Pen" data-id="7">Detail</a>
</ui-bind>

Form controls and visibility work the same way:

<ui-bind path="panel.open" config="show"><p id="a">Shown when the value is truthy</p></ui-bind>
<ui-bind path="panel.open" config="hide"><p id="b">Hidden when the value is truthy</p></ui-bind>
<ui-bind path="panel.locked" config="disabled"><input type="text" /><button>Save</button></ui-bind>
<ui-bind path="panel.accepted" config="checked"><input id="c" type="checkbox" /></ui-bind>
<ui-bind path="panel.note" config="value input"><input id="d" type="text" /></ui-bind>
SET('panel', { open: true, locked: true, accepted: true, note: 'Hello' });

Three attributes move the target of all commands outside the default element:

AttributeTarget
elementAny element on the page that matches the selector
parentThe closest parent that matches the selector
childThe elements inside <ui-bind> that match the selector
<h2></h2>
<ui-bind path="page.title" config="text" element="h2"></ui-bind>

<section>
    <ui-bind path="page.active" config=".active" parent="section"></ui-bind>
</section>

<ui-bind path="page.title" config="text" child=".name">
    <span class="name"></span>
    <span class="other">untouched</span>
</ui-bind>
SET('page', { title: 'Invoices', active: true });

Rendering lists

The template command compiles the content of a <script type="text/html"> or <template> element inside <ui-bind>. The bound value is available as value.

<ui-bind path="team.members" config="template">
    <script type="text/html">
        <ul>
            {{ foreach m in value }}
                <li>{{ m.name }}</li>
            {{ end }}
        </ul>
    </script>
</ui-bind>
SET('team.members', [{ name: 'Ana' }, { name: 'Peter' }]);

Each change of team.members renders the list again. For the template syntax, read Templates with Tangular.

Controlling when it renders

OptionEffect
! in front of the pathSkips rendering while the value is null or undefined
strictRenders only when the exact path changes, not its children
track:a,bFor changes of child paths, renders only for the listed keys
changesRenders only when the value is different from the last rendered one
delay:100Waits for the given number of milliseconds and renders the last value
onceRenders once and stops listening
check:FUNC.nameRenders only when the function returns a truthy value. Arguments: value, path, element
empty:textShows this text for an empty value. Without a text, it shows ---
<ui-bind id="a" path="!report.total" config="text:'Total: ' + value"></ui-bind>
<ui-bind id="b" path="report.data" config="text:value.a;strict"></ui-bind>
<ui-bind id="c" path="report.data" config="text:value.b;track:a"></ui-bind>
<ui-bind id="d" path="report.data" config="text:value.a;changes;exec:FUNC.rendered"></ui-bind>
<ui-bind id="e" path="report.search" config="text;delay:100"></ui-bind>
<ui-bind id="f" path="report.version" config="text;once"></ui-bind>
<ui-bind id="g" path="report.total" config="text;check:FUNC.positive"></ui-bind>
FUNC.positive = value => value > 0;
FUNC.rendered = value => console.log('rendered', value);

SET('report.data', { a: 1, b: 1 });
SET('report.version', 1);

After these changes:

SET('report.total', 5);
SET('report.data.a', 2);
SET('report.data.b', 2);
SET('report.version', 2);
ElementShowsWhy
#aTotal: 5It stayed empty until the value appeared
#b1strict ignores changes of report.data.a and report.data.b
#c1It renders value.b but tracks only the key a
#d2The value has changed
#f1once rendered only the first value
#g5The check passed. For -1, the element would keep showing 5

Two-way binding

<ui-bind> only displays data. To let the user change a value, use a component on the same path. A component receives changes of the model and writes the user's input back:

<ui-component name="textbox" path="profile.city"></ui-component>
<p>You live in <ui-bind path="profile.city" config="text"></ui-bind></p>
COMPONENT('textbox', function(self) {
    self.make = function() {
        self.html('<input type="text" />');
        self.autobind20();
    };
});

SET('profile.city', 'Bratislava');

The input starts with Bratislava. When the user types Kosice, the model and the paragraph change. A later SET('profile.city', 'Nitra') updates both the input and the paragraph. Creating components explains autobind20().

Reacting in code

To run code when a value changes, use WATCH() as described in Paths, or the exec command:

FUNC.log = function(value, path, element) {
    console.log(value, path.toString(), element.prop('tagName'));
};

SET('note.text', 'Hi');
Hi note.text UI-BIND
Made in Europe