ui-bind
The attributes and commands of the ui-bind element, and the binder object it creates.
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.
path="path"The attribute accepts these forms:
| Value | Meaning |
|---|---|
form.name | An absolute path. |
!form.name | The commands are skipped while the value is null or undefined. |
?.name | A path relative to the closest ui-plugin. |
*name | A path inside DEF.pathcommon. |
%name | A temporary path. |
@key | A value stored with data() of the parent component. @ alone is the value of that component and @config.key is a configuration key. |
.name | A path inside the model of a virtual binder. . alone is the whole model. |
form.name --> expression | Transforms 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.
<ui-bind path="!form.name" config="text">Waiting</ui-bind>
<ui-bind path="form.name --> value.toUpperCase()" config="text"></ui-bind>
<script>SET('form.name', 'peter');</script>
<ui-component name="card" config="title:Orders">
<ui-bind path="@config.title" config="text"></ui-bind>
</ui-component>
<script>COMPONENT('card', function(self) {});</script>
<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.
config="command:expression;command:expression"Commands are separated by a semicolon. Each command has the form name:expression. See Command syntax.
<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.
element="selector"The value is a jQuery selector evaluated in the whole document.
<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.
parent="selector"The value is a jQuery selector passed to closest().
<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.
child="selector"The attribute can be combined with parent.
<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.
plugin="path"The value is the path of the scope.
<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.
[!~]command [selector]:expressionThe expression can have these forms:
| Form | Meaning |
|---|---|
| omitted | The value itself, as in text. |
value + 1 | A JavaScript expression that contains the word value. The variables value, path and el are available. |
value => value + 1 | An arrow function. |
common.format | The path of a function called with value, path and el. |
?/format | A method of the plugin of the closest scope. |
The prefixes and separators are:
| Syntax | Meaning |
|---|---|
command selector:expression | Runs the command on the descendants that match the selector. |
text+title:value | Runs one expression for several commands. |
!command | Skips 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. |
~command | Runs the command also while the element is hidden by show, hide, visible or invisible. |
<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>
<ui-bind path="form.name" config="text+title:value"></ui-bind>
<script>SET('form.name', 'Peter');</script>
<ui-bind path="form.name" config="!text">Anonymous</ui-bind>
<script>
SET('form.name', 'Peter');
setTimeout(() => SET('form.name', null), 500);
</script>
<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.
text[:expression]
command
html[:expression]
Sets the HTML content.
html[:expression]The value is not escaped.
<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.
value[:expression]The short name val works too.
<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.
title[:expression]<ui-bind path="form.name" config="title"></ui-bind>
<script>SET('form.name', 'Peter');</script>
command
href[:expression]
Sets the href attribute.
href[:expression]<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.
src[:expression]<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.
attr name [selector]:expressionThe name of the attribute follows the command. A selector can follow the name.
<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.
.classname[ selector]:expressionWrite the name of the class after the dot.
<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.
class:classesSeparate several classes with a space.
<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.
show[:expression]Otherwise the element receives the hidden class. While hidden, the commands that follow are skipped unless they have the ~ prefix.
<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.
hide[:expression]The element receives the hidden class. The name hidden works too.
<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.
visible[:expression]The element receives the invisible class when the expression is falsy.
<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.
invisible[:expression]<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.
checked[:expression]<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.
disabled[:expression]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.
<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.
enabled[:expression]The short name enable works too. The default selector is the same as for disabled.
<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.
required selector:expressionAdd a selector, because the command has no default one.
<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.
config:expressionThe default selector is ui-component.
<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.
set[:expression]The default selector is ui-component. The component can write back to the same path.
<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.
setter:'selector/method',valueThe expression holds the arguments of SETTER(), evaluated with value, path and el.
<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.
template [selector][:selector-->attribute]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.
<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.
helpers:pathThe value is a path to an object with helpers, or to a function that returns one.
<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.
vbindarrayThe template is the first <script> or <template> inside the element. Paths inside the template start with a dot. See Virtual binder.
<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.
import:urlThe value is one of these:
| Value | Meaning |
|---|---|
A URL that starts with /, http:// or https:// | Imports the URL once. |
value or true | Moves the first inner <script> or <template> into the DOM. |
| An expression | Imports 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.
<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.
macro:name1,name2Separate several names with a comma. See 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.
exec:pathThe function receives value, path and el, and this is the element. It always receives the raw value. The name change works too.
<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.
init:pathThe function receives value, path and el.
<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.
refresh:pathThe function receives value, path and el.
<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.
click [selector]:pathThe 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.
<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.
format:formatA number sets the decimal places. Any other text is a date 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.
currency:nameThe name refers to a function in DEF.currencies.
<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.
helper:name[(arguments)]The value is passed as the first argument of the helper. Other arguments can be written in parentheses.
<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.
empty[:text]Without a value, the command uses DEF.empty, which is ---.
<ui-bind path="form.name" config="text;empty:Nothing"></ui-bind>
command
default:expression
Replaces null and undefined with a value.
default:expressionThe value is a JavaScript expression. The short name def works too.
<ui-bind path="form.name" config="text;default:'Anonymous'"></ui-bind>
command
selector:selector
Applies all commands to the matching descendants.
selector: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.
assign:path<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.
check:pathThe value is a path to a truthy value, or plugin/method, which receives value, path and el.
<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.
delay:millisecondsRepeated updates within the delay run once.
<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.
strictThe commands run only when the path itself or one of its parents changes.
<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.
track:field1,field2Separate several fields with a comma.
<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.
tracktype:type1,type2Separate several types with a comma.
<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.
changesThe value is compared with a hash.
<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.
readyThe commands run when all nested components are 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.
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.
focus:selectorThe value is a selector of descendants.
<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.
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.
binder.refresh()The binder is available as element.ui or through $(element).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.
binder.exec(value, path)| Parameter | Type | Description |
|---|---|---|
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.
var binder = $('ui-bind').binder();
binder.exec('Preview', 'form.name');
console.log($('ui-bind').text(), GET('form.name')); // Preview Peter