Spanish Total.js
English
Documentation For AI agents
Contents

ui-bind

The attributes and commands of the ui-bind element, and the binder object it creates.

.md GitHub
On this page

The <ui-bind> element watches a path and updates the DOM when the value changes. Its config attribute holds a list of commands. Data binding explains the concept.

API

property

path="path"

Sets the path that the element watches.

The attribute accepts these forms:

ValueMeaning
form.nameAn absolute path.
!form.nameThe commands are skipped while the value is null or undefined.
?.nameA path relative to the closest ui-plugin.
*nameA path inside DEF.pathcommon.
%nameA temporary path.
@keyA value stored with data() of the parent component. @ alone is the value of that component and @config.key is a configuration key.
.nameA path inside the model of a virtual binder. . alone is the whole model.
form.name --> expressionTransforms the value with an expression that receives value.

When the element has a path and no config, the path must contain a function, which is called with the element.

Skipping empty values
<ui-bind path="!form.name" config="text">Waiting</ui-bind>
Transforming the value
<ui-bind path="form.name --> value.toUpperCase()" config="text"></ui-bind>
<script>SET('form.name', 'peter');</script>
Reading data of the parent component
<ui-component name="card" config="title:Orders">
    <ui-bind path="@config.title" config="text"></ui-bind>
</ui-component>
<script>COMPONENT('card', function(self) {});</script>
Calling a function with the element
<ui-bind path="common.init" id="box"></ui-bind>
<script>
    window.common = { init: element => console.log(element.attr('id')) };  // box
</script>
property

config="command:expression;command:expression"

Holds the commands of the element.

Commands are separated by a semicolon. Each command has the form name:expression. See Command syntax.

Two commands
<ui-bind path="form.age" config="text:value + ' years';.adult:value >= 18"></ui-bind>
<script>SET('form.age', 20);</script>
property

element="selector"

Applies the commands to another element.

The value is a jQuery selector evaluated in the whole document.

Updating an element elsewhere
<ui-bind path="form.name" element="#target" config="text"></ui-bind>
<div id="target"></div>
<script>SET('form.name', 'Peter');</script>
property

parent="selector"

Applies the commands to the closest matching parent.

The value is a jQuery selector passed to closest().

Updating a parent element
<div class="row">
    <ui-bind path="form.name" parent=".row" config="title:value"></ui-bind>
</div>
<script>SET('form.name', 'Peter');</script>
property

child="selector"

Applies the commands to the first matching descendant.

The attribute can be combined with parent.

Updating a child element
<ui-bind path="form.name" child="b" config="text">
    <b></b>
</ui-bind>
<script>SET('form.name', 'Peter');</script>
property

plugin="path"

Creates a scope directly on the element.

The value is the path of the scope.

A scope on the element
<ui-bind plugin="users" path="?.name" config="text"></ui-bind>

<script>
    PLUGIN('users', function(exports) {
        exports.set('name', 'Peter');
    });
</script>
type

[!~]command [selector]:expression

A command is a name followed by an optional expression.

The expression can have these forms:

FormMeaning
omittedThe value itself, as in text.
value + 1A JavaScript expression that contains the word value. The variables value, path and el are available.
value => value + 1An arrow function.
common.formatThe path of a function called with value, path and el.
?/formatA method of the plugin of the closest scope.

The prefixes and separators are:

SyntaxMeaning
command selector:expressionRuns the command on the descendants that match the selector.
text+title:valueRuns one expression for several commands.
!commandSkips the command while the value is null or undefined. On the element itself, text, html, title, value, src, checked, disabled and enabled then return to their original content.
~commandRuns the command also while the element is hidden by show, hide, visible or invisible.
Expression forms
<ui-bind path="form.name" config="text b:value.length;title:n => n + '!';.long:common.long">
    <b></b>
</ui-bind>
<script>
    window.common = { long: value => value.length > 3 };
    SET('form.name', 'Peter');
</script>
One expression for two commands
<ui-bind path="form.name" config="text+title:value"></ui-bind>
<script>SET('form.name', 'Peter');</script>
Restoring the original content
<ui-bind path="form.name" config="!text">Anonymous</ui-bind>
<script>
    SET('form.name', 'Peter');
    setTimeout(() => SET('form.name', null), 500);
</script>
Updating a hidden element
<ui-bind path="form.name" config="~text;hide:value">Anonymous</ui-bind>
<script>SET('form.name', 'Peter');</script>
command

text[:expression]

Sets the text content.

The value is escaped. The format, currency, helper and empty commands change the output.

Using text
<ui-bind path="form.name" config="text"></ui-bind>
<script>SET('form.name', '<b>Peter</b>');</script>
command

html[:expression]

Sets the HTML content.

The value is not escaped.

Using html
<ui-bind path="form.name" config="html:'<b>' + value + '</b>'"></ui-bind>
<script>SET('form.name', 'Peter');</script>
command

value[:expression]

Sets the value of a form field.

The short name val works too.

Using value
<ui-bind path="form.name" config="value input">
    <input />
</ui-bind>
<script>
    SET('form.name', 'Peter');
    setTimeout(() => console.log($('input').val()), 500);  // Peter
</script>
command

title[:expression]

Sets the title attribute.
Using title
<ui-bind path="form.name" config="title"></ui-bind>
<script>SET('form.name', 'Peter');</script>
command

href[:expression]

Sets the href attribute.
Using href
<ui-bind path="form.id" config="href a:'/users/' + value + '/'">
    <a>Detail</a>
</ui-bind>
<script>SET('form.id', 5);</script>
command

src[:expression]

Sets the src attribute.
Using src
<ui-bind path="form.photo" config="src img">
    <img />
</ui-bind>
<script>SET('form.photo', 'data:,');</script>
command

attr name [selector]:expression

Sets any attribute.

The name of the attribute follows the command. A selector can follow the name.

Using attr
<ui-bind path="form.id" config="attr data-id:value;attr data-ref b:value">
    <b></b>
</ui-bind>
<script>SET('form.id', 5);</script>
command

.classname[ selector]:expression

Toggles a CSS class by the result of the expression.

Write the name of the class after the dot.

Toggling classes
<ui-bind path="form.age" config=".adult:value >= 18;.minor:value < 18"></ui-bind>
<script>SET('form.age', 20);</script>
command

class:classes

Toggles CSS classes once, after the first update.

Separate several classes with a space.

Using class
<ui-bind path="form.name" config="class:ready"></ui-bind>
<script>SET('form.name', 'Peter');</script>
command

show[:expression]

Shows the element when the expression is truthy.

Otherwise the element receives the hidden class. While hidden, the commands that follow are skipped unless they have the ~ prefix.

Using show
<ui-bind path="form.age" config="show:value >= 18">Adult</ui-bind>
<script>SET('form.age', 10);</script>
command

hide[:expression]

Hides the element when the expression is truthy.

The element receives the hidden class. The name hidden works too.

Using hide
<ui-bind path="form.age" config="hide:value >= 18">Minor</ui-bind>
<script>SET('form.age', 20);</script>
command

visible[:expression]

Keeps the layout space when the expression is falsy.

The element receives the invisible class when the expression is falsy.

Using visible
<ui-bind path="form.age" config="visible:value >= 18">Adult</ui-bind>
<script>SET('form.age', 10);</script>
command

invisible[:expression]

Adds the invisible class when the expression is truthy.
Using invisible
<ui-bind path="form.age" config="invisible:value >= 18">Minor</ui-bind>
<script>SET('form.age', 20);</script>
command

checked[:expression]

Sets the checked property of a field.
Using checked
<ui-bind path="form.active" config="checked input">
    <input type="checkbox" />
</ui-bind>
<script>
    SET('form.active', true);
    setTimeout(() => console.log($('input').prop('checked')), 500);  // true
</script>
command

disabled[:expression]

Disables fields and components when the expression is truthy.

The short name disable works too. Without a selector, the command applies to ui-component, input, textarea, select and button inside the element. Components receive the disabled configuration key.

Using disabled
<ui-bind path="form.locked" config="disabled">
    <button>Save</button>
</ui-bind>
<script>SET('form.locked', true);</script>
command

enabled[:expression]

Enables fields and components when the expression is truthy.

The short name enable works too. The default selector is the same as for disabled.

Using enabled
<ui-bind path="form.valid" config="enabled">
    <button>Save</button>
</ui-bind>
<script>SET('form.valid', false);</script>
command

required selector:expression

Sets the required configuration key of a component.

Add a selector, because the command has no default one.

Using required
<ui-bind path="form.strict" config="required ui-component:value">
    <ui-component name="box"></ui-component>
</ui-bind>
<script>
    COMPONENT('box', function(self) {
        self.configure = (key, value) => key === 'required' && console.log(value);  // true
    });
    SET('form.strict', true);
</script>
command

config:expression

Reconfigures components with the result of the expression.

The default selector is ui-component.

Using config
<ui-bind path="form.title" config="config:'title:' + value">
    <ui-component name="box"></ui-component>
</ui-bind>
<script>
    COMPONENT('box', function(self) {
        self.configure = (key, value) => key === 'title' && console.log(value);  // Orders
    });
    SET('form.title', 'Orders');
</script>
command

set[:expression]

Passes the result of the expression to the setter of a component without a path.

The default selector is ui-component. The component can write back to the same path.

Using set
<ui-bind path="form.name" config="set:value + '!'">
    <ui-component name="box"></ui-component>
</ui-bind>
<script>
    COMPONENT('box', function(self) {
        self.setter = value => self.text(value);
    });
    SET('form.name', 'Peter');
</script>
command

setter:'selector/method',value

Calls a method of matching components.

The expression holds the arguments of SETTER(), evaluated with value, path and el.

Using setter
<ui-bind path="form.name" config="setter:'box/show',value">
    <ui-component name="box"></ui-component>
</ui-bind>
<script>
    COMPONENT('box', function(self) {
        self.show = value => value && console.log(value);  // Peter
    });
    setTimeout(() => SET('form.name', 'Peter'), 500);
</script>
command

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

Renders a Tangular template.

The template is the first <script> or <template> inside the element. The model has the properties value and path.

The optional template:selector-->attribute form updates only the changed child elements. The selector finds the items and the attribute identifies them. Read more in Templates with Tangular.

Using template
<ui-bind path="form.items" config="template">
    <script type="text/html">
        {{ foreach m in value }}<li>{{ m }}</li>{{ end }}
    </script>
</ui-bind>
<script>SET('form.items', ['A', 'B']);</script>
command

helpers:path

Passes Tangular helpers to the template.

The value is a path to an object with helpers, or to a function that returns one.

Using helpers
<ui-bind path="form.name" config="template;helpers:common.helpers">
    <script type="text/html">{{ value | shout }}</script>
</ui-bind>
<script>
    window.common = { helpers: { shout: value => value + '!' } };
    SET('form.name', 'Peter');
</script>
command

vbindarray

Renders an array with a virtual binder per item.

The template is the first <script> or <template> inside the element. Paths inside the template start with a dot. See Virtual binder.

Using vbindarray
<ui-bind path="form.users" config="vbindarray">
    <script type="text/html">
        <div><ui-bind path=".name" config="text"></ui-bind></div>
    </script>
</ui-bind>
<script>SET('form.users', [{ name: 'Peter' }, { name: 'Anna' }]);</script>
command

import:url

Loads content into the element.

The value is one of these:

ValueMeaning
A URL that starts with /, http:// or https://Imports the URL once.
value or trueMoves the first inner <script> or <template> into the DOM.
An expressionImports the URL returned by the path value, once per URL.

The import waits until the element is visible, so it can be combined with show.

Inserting a template when the element is shown
<ui-bind path="form.open" config="show;import:value">
    <script type="text/html"><b>Loaded</b></script>
</ui-bind>
<script>
    setTimeout(() => SET('form.open', true), 500);
</script>
command

macro:name1,name2

Attaches macros to the element.

Separate several names with a comma. See Macro.

Using macro
<ui-bind path="form.name" config="macro:shout"></ui-bind>
<script>
    MACRO('shout', function(self) {
        self.setter = value => self.text(value + '!');
    });
    SET('form.name', 'Peter');
</script>
command

exec:path

Calls a function on each update.

The function receives value, path and el, and this is the element. It always receives the raw value. The name change works too.

Using exec
<ui-bind path="form.name" config="exec:common.onchange"></ui-bind>
<script>
    window.common = { onchange: (value, path) => value && console.log(value, path) };  // Peter form.name
    setTimeout(() => SET('form.name', 'Peter'), 500);
</script>
command

init:path

Calls a function at the beginning of each update.

The function receives value, path and el.

Using init
<ui-bind path="form.name" config="init:common.oninit"></ui-bind>
<script>
    window.common = { oninit: value => value && console.log(value) };  // Peter
    SET('form.name', 'Peter');
</script>
command

refresh:path

Calls a function on each update while the element is visible.

The function receives value, path and el.

Using refresh
<ui-bind path="form.name" config="refresh:common.onrefresh"></ui-bind>
<script>
    window.common = { onrefresh: value => value && console.log(value) };  // Peter
    SET('form.name', 'Peter');
</script>
command

click [selector]:path

Calls a function when the element is clicked.

The function receives the jQuery element, the event, the current value and the path. A selector delegates the click to the matching descendants. Disabled inputs and buttons are ignored.

Using click
<ui-bind path="form.name" config="click button:common.onclick">
    <button>Save</button>
</ui-bind>
<script>
    window.common = {
        onclick: (element, event, value, path) => console.log(element.text(), value, path)  // Save Peter form.name
    };
    SET('form.name', 'Peter');
    setTimeout(() => $('button').click(), 500);
</script>
command

format:format

Formats a number or a date.

A number sets the decimal places. Any other text is a date format.

Using format
<ui-bind path="form.price" config="text;format:2"></ui-bind>
<script>SET('form.price', 1250);</script>
command

currency:name

Formats a number with a currency.

The name refers to a function in DEF.currencies.

Using currency
<ui-bind path="form.price" config="text;currency:eur"></ui-bind>
<script>
    DEF.currencies.eur = value => value.format(2) + ' EUR';
    SET('form.price', 1250);
</script>
command

helper:name[(arguments)]

Transforms the output with a Tangular helper.

The value is passed as the first argument of the helper. Other arguments can be written in parentheses.

Using helper
<ui-bind path="form.name" config="text;helper:wrap('*')"></ui-bind>
<script>
    Thelpers.wrap = (value, chars) => chars + value + chars;
    SET('form.name', 'Peter');
</script>
command

empty[:text]

Sets the text used for an empty value.

Without a value, the command uses DEF.empty, which is ---.

Using empty
<ui-bind path="form.name" config="text;empty:Nothing"></ui-bind>
command

default:expression

Replaces null and undefined with a value.

The value is a JavaScript expression. The short name def works too.

Using default
<ui-bind path="form.name" config="text;default:'Anonymous'"></ui-bind>
command

selector:selector

Applies all commands to the matching descendants.
Using selector
<ui-bind path="form.name" config="selector:b;text">
    <b></b><b></b>
</ui-bind>
<script>SET('form.name', 'Peter');</script>
command

assign:path

Writes the jQuery element to a path.
Using assign
<ui-bind id="box" path="form.name" config="assign:common.box;text"></ui-bind>
<script>
    setTimeout(() => console.log(GET('common.box').attr('id')), 500);  // box
</script>
command

check:path

Runs the commands only when a condition passes.

The value is a path to a truthy value, or plugin/method, which receives value, path and el.

Using check
<ui-bind path="form.name" config="check:form.ready;text">Waiting</ui-bind>
<script>SET('form.name', 'Peter');</script>
command

delay:milliseconds

Delays the update in milliseconds.

Repeated updates within the delay run once.

Using delay
<ui-bind path="form.name" config="delay:500;text"></ui-bind>
<script>
    SET('form.name', 'Peter');
    setTimeout(() => console.log($('ui-bind').text() || 'empty'), 200);  // empty
    setTimeout(() => console.log($('ui-bind').text()), 1100);  // Peter
</script>
command

strict

Ignores changes of nested paths.

The commands run only when the path itself or one of its parents changes.

Using strict
<ui-bind path="form.user" config="strict;exec:common.update"></ui-bind>
<script>
    window.common = { update: (value, path) => console.log(path) };
    SET('form.user', { name: 'Peter' });
    setTimeout(() => SET('form.user.name', 'Anna'), 500);
</script>
command

track:field1,field2

Runs the commands only for selected nested fields.

Separate several fields with a comma.

Using track
<ui-bind path="form.user" config="track:name;exec:common.update"></ui-bind>
<script>
    window.common = { update: (value, path) => console.log(path) };
    SET('form.user', { name: 'Peter', age: 20 });
    setTimeout(() => SET('form.user.age', 21), 500);
    setTimeout(() => SET('form.user.name', 'Anna'), 700);
</script>
command

tracktype:type1,type2

Runs the commands only for selected update types.

Separate several types with a comma.

Using tracktype
<ui-bind path="form.age" config="tracktype:2;exec:common.update"></ui-bind>
<script>
    window.common = { update: value => value && console.log(value) };
    setTimeout(() => SET('form.age', 10, 1), 500);
    setTimeout(() => SET('form.age', 20, 2), 700);
</script>
command

changes

Skips the commands when the value has not changed.

The value is compared with a hash.

Using changes
<ui-bind path="form.age" config="changes;exec:common.update"></ui-bind>
<script>
    window.common = { update: value => value && console.log(value) };
    setTimeout(() => SET('form.age', 10), 500);
    setTimeout(() => UPD('form.age'), 700);
</script>
command

ready

Waits for the components nested in the element.

The commands run when all nested components are ready.

Using ready
<ui-bind path="form.name" config="ready;exec:common.update">
    <ui-component name="box"></ui-component>
</ui-bind>
<script>
    window.common = { update: () => console.log(FIND('box') ? 'found' : 'missing') };  // found
    COMPONENT('box', function(self) {});
</script>
command

once

Runs the commands only one time.
Using once
<ui-bind path="form.name" config="once;text"></ui-bind>
<script>
    SET('form.name', 'Peter');
    setTimeout(() => SET('form.name', 'Anna'), 700);
</script>
command

focus:selector

Moves the focus to a field after each update.

The value is a selector of descendants.

Focusing a field
<ui-bind path="form.name" config="focus:input">
    <input />
</ui-bind>
<script>
    SET('form.name', 'Peter');
</script>
command

resize

Calls the resize method of nested components after each update.
Using resize
<ui-bind path="form.name" config="resize">
    <ui-component name="box"></ui-component>
</ui-bind>
<script>
    COMPONENT('box', function(self) {
        self.resize = () => console.log('resized');  // resized
    });
    setTimeout(() => SET('form.name', 'Peter'), 500);
</script>
method

binder.refresh()

Runs the commands again with the current value of the path.

The binder is available as element.ui or through $(element).binder().

Refreshing a binder
var binder = $('ui-bind').binder();
window.form.name = 'Anna';
binder.refresh();
console.log($('ui-bind').text());  // Anna
method

binder.exec(value, path)

Runs the commands with a custom value.
ParameterTypeDescription
value Object The value passed to the commands.
path String The path passed to the commands.

The value is not written to the data model.

Running the commands with a value
var binder = $('ui-bind').binder();
binder.exec('Preview', 'form.name');
console.log($('ui-bind').text(), GET('form.name'));  // Preview Peter
Made in Europe