Spanish Total.js
English
Documentation For AI agents
Contents

ui-bind

The attributes and the commands of the ui-bind element.

.md GitHub
On this page

The <ui-bind> element updates the page each time the value at a path changes. The config attribute holds a list of commands separated with ;. The Data binding guide explains how to use it.

A command has the form name:expression, or name selector:expression to act on the elements found by a selector inside the target. Without an expression, the command uses the value at the path. The expression is JavaScript and can use value, path, flags, element and el (the same as element). Join command names with + to give them the same expression, for example text+title:value. Write \\; for a semicolon inside an expression. All commands run once when the element is created, with the current value at the path. An error in an expression is written to the console and the commands continue.

API

property

path="path"

The path to observe.

A ? is replaced with the path of the parent plugin. With a ! at the beginning (!common.user), the commands do not run while the value is null or undefined.

Skip empty values
<ui-bind path="!common.user" config="text:value.name"></ui-bind>

<script>
SET('common.user', { name: 'Peter' });
console.log($('ui-bind').text());
// logs: Peter
</script>
property

config="commands"

The commands, separated with ;.

The values are kept as strings, because they are compiled as JavaScript.

property

element="selector"

Runs the commands on the elements found by a selector in the whole document.
Update an element elsewhere in the page
<h1 id="title"></h1>
<ui-bind path="common.title" element="#title" config="text"></ui-bind>

<script>
SET('common.title', 'Orders');
console.log($('#title').text());
// logs: Orders
</script>
property

child="selector"

Runs the commands on the elements found by a selector inside the element.

The value command needs this attribute or a selector to reach an input.

Fill an input
<ui-bind path="common.email" child="input" config="value"><input /></ui-bind>

<script>
SET('common.email', 'info@example.com');
console.log($('input').val());
// logs: info@example.com
</script>
property

parent="selector"

Runs the commands on the closest parent found by a selector.
Change a parent element
<div class="row">
    <ui-bind path="common.active" parent=".row" config=".selected"></ui-bind>
</div>

<script>
SET('common.active', true);
console.log($('.row').attr('class'));
// logs: row selected
</script>
property

plugin="path"

Creates a plugin on the element itself, and a ? in the path refers to it.
A binder with its own plugin
<ui-bind plugin="clock" path="?.time" config="text"></ui-bind>

<script>
PLUGIN('clock', function(exports) {
    exports.make = () => exports.set('time', '12:00');
});

console.log($('ui-bind').text());
// logs: 12:00
</script>
command

.name:expression

Toggles the CSS class name according to the result of the expression.
Toggle classes
<ui-bind path="common.count" config=".empty:!value;.many:value > 10"></ui-bind>

<script>
SET('common.count', 20);
console.log($('ui-bind').attr('class'));
// logs: many
</script>
command

text:expression

Sets the text of the element. HTML is escaped.

A null, undefined, false or empty-string result writes the value of the empty command, and 0 is written as 0. Nothing is written while the element is hidden by show, hide, visible or invisible, unless forcerender allows it.

Show a text
<ui-bind path="common.name" config="text:value.toUpperCase()"></ui-bind>

<script>
SET('common.name', 'anna');
console.log($('ui-bind').text());
// logs: ANNA
</script>
command

html:expression

Sets the HTML content of the element.

It follows the same rules as text, without escaping.

Render markup
<ui-bind path="common.name" config="html:'<b>' + value + '</b>'"></ui-bind>

<script>
SET('common.name', 'Anna');
console.log($('ui-bind').html());
// logs: <b>Anna</b>
</script>
command

value selector:expression

Sets the value of a form control.

Use a selector or the child attribute, because the <ui-bind> element is not a control itself.

Fill an input with a selector
<ui-bind path="common.city" config="value input"><input /></ui-bind>

<script>
SET('common.city', 'Bratislava');
console.log($('input').val());
// logs: Bratislava
</script>
command

title:expression

Sets the title attribute.
Text and title together
<ui-bind path="common.name" config="text+title:value"></ui-bind>

<script>
ON('ready', function() {
    SET('common.name', 'Anna');
    console.log($('ui-bind').text(), $('ui-bind').attr('title'));
});
// logs: Anna Anna
</script>
command

href:expression

Sets the href attribute.
command

src:expression

Sets the src attribute.
Set attributes with selectors
<ui-bind path="common.user" config="href a:'/users/' + value.id;title a:value.name;src img:value.photo">
    <a><img /></a>
</ui-bind>

<script>
SET('common.user', { id: 7, name: 'Anna', photo: '/a.png' });
console.log($('ui-bind').html().trim());
// logs: <a href="/users/7" title="Anna"><img src="/a.png"></a>
</script>
command

attr name [selector]:expression

Sets the attribute name.
Set a custom attribute
<ui-bind path="common.id" config="attr data-id:value"></ui-bind>

<script>
SET('common.id', 5);
console.log($('ui-bind').attr('data-id'));
// logs: 5
</script>
command

show:expression

Removes the CSS class hidden when the result is truthy and adds it otherwise.
Show and hide
<ui-bind path="common.items" config="show:value > 0">Items</ui-bind>
<ui-bind path="common.items" config="hide:value > 0">Empty</ui-bind>

<script>
SET('common.items', 3);
console.log($('ui-bind').eq(0).hclass('hidden'), $('ui-bind').eq(1).hclass('hidden'));
// logs: false true
</script>
command

hide:expression

Adds the CSS class hidden when the result is truthy and removes it otherwise.
command

visible:expression

Removes the CSS class invisible when the result is truthy and adds it otherwise.
command

invisible:expression

Adds the CSS class invisible when the result is truthy and removes it otherwise.
Keep the space of the element
<ui-bind path="common.ready" config="invisible:!value">Loaded</ui-bind>

<script>
console.log($('ui-bind').attr('class'));
// logs: invisible
SET('common.ready', true);
console.log($('ui-bind').hclass('invisible'));
// logs: false
</script>
command

forcerender:expression

Allows text, html and template to render while the element is hidden, when the result is truthy.
Render into a hidden element
<ui-bind id="a" path="common.name" config="show:false;text"></ui-bind>
<ui-bind id="b" path="common.name" config="show:false;forcerender:true;text"></ui-bind>

<script>
SET('common.name', 'Anna');
console.log($('#a').text() === '', $('#b').text());
// logs: true Anna
</script>
command

template:[{selector}] [selector --> attribute]

Renders a Tangular template with the value.

The template comes from a <script> or <template> element inside the target, from the HTML of the target, or from the element in {selector}. The model contains value, path and element. The text SCR in a template is replaced with script.

With selector --> attribute, only the elements matching the selector are updated. Existing elements with the same attribute value stay in the page. Without an attribute, whole elements are compared. See Templates with Tangular.

Render a list
<ui-bind path="common.users" config="template">
    <script type="text/html">
        {{ foreach m in value }}<b>{{ m }}</b>{{ end }}
    </script>
</ui-bind>

<script>
SET('common.users', ['Anna', 'Peter']);
console.log($('ui-bind').html().trim());
// logs: <b>Anna</b><b>Peter</b>
</script>
Use an external template and update only changed rows
<script type="text/html" id="row"><div data-id="{{ value }}">{{ value }}</div></script>
<ui-bind path="common.id" config="template:{#row} div --> data-id"></ui-bind>

<script>
SET('common.id', 1);
var first = $('ui-bind div')[0];
SET('common.id', 1);
console.log(first === $('ui-bind div')[0]);
// logs: true
</script>
command

helpers:path

Sets the object with the Tangular helpers for the template command.
Custom helpers
<ui-bind path="common.name" config="template;helpers:FUNC.helpers">
    <template>{{ value | shout }}</template>
</ui-bind>

<script>
FUNC.helpers = {
    shout: value => (value || '').toUpperCase() + '!'
};

SET('common.name', 'anna');
console.log($('ui-bind').html().trim());
// logs: ANNA!
</script>
command

format:value

Formats a Date value for the text, html, value, title, href, src and attr commands.
Format a date
<ui-bind path="common.date" config="text;format:dd.MM.yyyy"></ui-bind>

<script>
SET('common.date', new Date(2024, 0, 15));
console.log($('ui-bind').text());
// logs: 15.01.2024
</script>
command

empty:text

Sets the text written by text, html, value, title, href and src when the result is null, undefined, false or an empty string.

Without a value, the text in DEF.empty is used (---).

Show a placeholder
<ui-bind path="common.note" config="text;empty:No note"></ui-bind>

<script>
console.log($('ui-bind').text());
// logs: No note
</script>
command

class:name

Toggles a CSS class on the <ui-bind> element once, after the commands run for the first time.
Mark the element as rendered
<ui-bind path="common.name" config="text;class:rendered"></ui-bind>

<script>
console.log($('ui-bind').attr('class'));
// logs: rendered
</script>
command

exec:path

Calls a function with the value, the path and the target element.

A ? in the value is replaced with the path of the parent plugin. The form ?/method calls a method of the plugin.

Call a function on each change
<ui-bind path="common.total" config="exec:FUNC.total"></ui-bind>

<script>
FUNC.total = function(value, path, element) {
    value && console.log(value, path.toString(), element[0].tagName);
};

SET('common.total', 10);
// logs: 10 common.total UI-BIND
</script>
command

refresh:path

Calls a function with the value, the path and the target element, in the same way as exec.
command

focus:selector

Moves the focus to the first visible element found by the selector, 500 milliseconds after the change.

The focus is not moved on mobile devices.

Focus an input
<ui-bind path="common.open" config="focus:input"><input id="name" /></ui-bind>

<script>
setTimeout(() => console.log(document.activeElement.id), 700);
// logs: name
</script>
command

checked:expression

Sets the checked state of the checkboxes and radio buttons inside the target.
Check a checkbox
<ui-bind path="common.agree" config="checked"><input type="checkbox" /></ui-bind>

<script>
SET('common.agree', true);
console.log($('input')[0].checked);
// logs: true
</script>
command

disabled:expression

Disables the form controls and the components inside the target when the result is truthy.

Form controls are input, select, textarea and button. Components receive the disabled config key.

Disable a form
<ui-bind path="common.locked" config="disabled">
    <input />
    <ui-component name="field"></ui-component>
</ui-bind>

<script>
COMPONENT('field', function(self) {});

SET('common.locked', true);
console.log($('input').prop('disabled'), $('ui-component').hclass('ui-disabled'));
// logs: true true
</script>
command

enabled:expression

Enables the form controls and the components inside the target when the result is truthy, and disables them otherwise.
Enable a button
<ui-bind path="common.valid" config="enabled"><button>Save</button></ui-bind>

<script>
console.log($('button').prop('disabled'));
// logs: true
SET('common.valid', true);
console.log($('button').prop('disabled'));
// logs: false
</script>
command

required:expression

Sets the required config key of the components inside the target.
Require a field
<ui-bind path="common.company" config="required"><ui-component name="field"></ui-component></ui-bind>

<script>
COMPONENT('field', function(self) {});

SET('common.company', true);
console.log($('ui-component').component().config.required);
// logs: true
</script>
command

assign:path

Writes the instance of the binder to a path.

The instance has the method exec(value, path, [flags]), which runs the commands with a given value.

Run the commands from code
<ui-bind path="common.name" config="text;assign:common.binder"></ui-bind>

<script>
common.binder.exec('Manual', 'common.name');
console.log($('ui-bind').text());
// logs: Manual
</script>
command

once

Runs the commands only one time.

With the ! prefix in the path, the commands run for the first value that is not empty. Without it, they run with the value that exists when the element is created.

Render the first value
<ui-bind path="!common.name" config="once;text"></ui-bind>

<script>
SET('common.name', 'first');
SET('common.name', 'second');
console.log($('ui-bind').text());
// logs: first
</script>
command

strict

Ignores changes made in nested paths.
React only to the path itself
<ui-bind path="common.user" config="strict;text:value.name"></ui-bind>

<script>
SET('common.user', { name: 'Anna' });
SET('common.user.name', 'Peter');
console.log($('ui-bind').text());
// logs: Anna
</script>
command

track:name[,name]

Limits changes in nested paths to the listed property names.

A change of the observed path itself, or of a parent path, always runs the commands.

Track one property
<ui-bind path="common.user" config="track:name;text:value.name + ' ' + value.age"></ui-bind>

<script>
SET('common.user.age', 30);
console.log($('ui-bind').text());
// logs: Anna 20
SET('common.user.name', 'Peter');
console.log($('ui-bind').text());
// logs: Peter 30
</script>
command

changes

Runs the commands only when the value differs from the previous one.
Ignore identical values
<ui-bind path="common.total" config="changes;exec:FUNC.log"></ui-bind>

<script>
FUNC.log = value => value && console.log(value);

SET('common.total', 1);
SET('common.total', 1);
SET('common.total', 2);
// logs: 1
// logs: 2
</script>
command

check:path

Runs the commands only when the function at the path returns a truthy value.

The function receives the value, the path and the <ui-bind> element.

Filter the values
<ui-bind path="common.age" config="check:FUNC.adult;text"></ui-bind>

<script>
FUNC.adult = value => value >= 18;

SET('common.age', 15);
console.log($('ui-bind').text() === '');
// logs: true
SET('common.age', 20);
console.log($('ui-bind').text());
// logs: 20
</script>
command

delay:milliseconds

Waits for the given time and runs the commands with the last value.

Without a value, the delay is 100 milliseconds.

Group quick changes
<ui-bind path="common.search" config="delay:50;text"></ui-bind>

<script>
SET('common.search', 'a');
SET('common.search', 'ab');
console.log($('ui-bind').text() === '');
// logs: true
setTimeout(() => console.log($('ui-bind').text()), 200);
// logs: ab
</script>
Made in Europe