Documentation

class Binding

A class that represents a property binding.

class Binding<T = any>;

Description

A binding connects an object (an instance of ObservableObject) with one of the properties of one of another object: either a specified source object, or one of the target object’s containing (attached) objects. Bindings can be used to update properties on the target object, keeping the bound property in sync with the original property.

For example, given an object A with property p, a binding can be used on an attached object B to update target property q. When the value of p changes, the same value is set on q immediately. This is considered a one-way data binding, since direct updates on property q don’t affect the source property p at all.

To make bindings work across a chain — or technically, a tree structure — of attached objects, bindings keep track of object attachments to the target object (i.e. object B) all the way up the chain, to find a matching source property. Therefore, a binding on C, itself attached from B, may also read property values from A.

Instead of a single source property, bindings can also specify a source property ‘path’ using dots to separate property names: a path of p.foo.bar first watches p, then (if p refers to an object) its property foo, and so on — going first up the tree structure to find the object containing p, and then down again to find the rest.

As a concrete example, a binding can be used to update the text property of a UIText view, with the value of a string property someText of the activity. Or perhaps the property name of a user object referenced by the activity (see example below). Whenever the data in the activity changes, so does the text.

Creating bindings — To create a binding, use the Binding constructor to bind a single property, use methods like Binding.all(), Binding.any(), or Binding.none() to combine bindings, or use the Binding.fmt() function to bind a string composed using a format string and one or more embedded bindings.

Applying bindings — To use a binding, pass it to a UI view builder function, e.g. UI.Text(v.bind("someText")). To apply a binding to any other observable object directly, use the ObservableObject.observe() method.

Adding transformations — To convert the value of the original property, or to combine multiple bindings using boolean operations (and/or), use one of the Binding methods such as map(), Binding.all(), and Binding.any().

Constructor

Type members

Static members

Instance members