Data binding
How to render values of the model into the page with ui-bind and keep inputs and the model in sync.
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:
| Variable | Description |
|---|---|
value | The value at the path |
element, el | The target element as a jQuery object |
flags | The flags of the operation |
path | The 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
| Command | Effect |
|---|---|
text | Sets the text content |
html | Sets the HTML content |
value | Sets the value of a form control |
title, href, src | Sets the attribute of the same name |
attr name | Sets the attribute name |
.name | Adds the CSS class name when the result is truthy and removes it otherwise |
show | Adds the class hidden when the result is falsy |
hide | Adds the class hidden when the result is truthy |
visible | Adds the class invisible when the result is falsy |
invisible | Adds the class invisible when the result is truthy |
disabled | Disables the form controls inside the target when the result is truthy |
enabled | Disables the form controls inside the target when the result is falsy |
checked | Checks the inputs inside the target when the result is truthy |
template | Renders a Tangular template |
exec | Calls 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:
| Attribute | Target |
|---|---|
element | Any element on the page that matches the selector |
parent | The closest parent that matches the selector |
child | The 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
| Option | Effect |
|---|---|
! in front of the path | Skips rendering while the value is null or undefined |
strict | Renders only when the exact path changes, not its children |
track:a,b | For changes of child paths, renders only for the listed keys |
changes | Renders only when the value is different from the last rendered one |
delay:100 | Waits for the given number of milliseconds and renders the last value |
once | Renders once and stops listening |
check:FUNC.name | Renders only when the function returns a truthy value. Arguments: value, path, element |
empty:text | Shows 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);| Element | Shows | Why |
|---|---|---|
#a | Total: 5 | It stayed empty until the value appeared |
#b | 1 | strict ignores changes of report.data.a and report.data.b |
#c | 1 | It renders value.b but tracks only the key a |
#d | 2 | The value has changed |
#f | 1 | once rendered only the first value |
#g | 5 | The 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