class ObservableObject
The base class of all observable objects, which can be placed into a tree structure to enable event handling and data binding.
class ObservableObject;
Description
The ObservableObject class is the core construct that powers framework concepts such as event handling and property binding. Many of the other classes are derived from ObservableObject, including ObservableList. This class provides the following features:
Object lifecycle — The unlink() method marks an object as stale, and disables the other features below. Objects can add additional lifecycle behavior by overriding the beforeUnlink() method.
Attaching objects — Observable objects are meant to be linked together to form a directed graph, i.e. a tree structure where one object can be ‘attached’ to only one other observable object: its parent, origin, or containing object. In turn, several objects can be attached to every object, which makes up a hierarchical structure.
- Objects can be attached using the attach() method. A callback function can be used to listen for events and unlinking.
- When the origin object is unlinked, its attached objects are unlinked as well.
Events — The emit() method ‘emits’ an event from an observable object. Events are instances of ObservableEvent, which can be handled using a callback provided to the listen() or ObservableObject.attach() methods. Typically, events are handled by their parent object (the object they’re attached to). This enables event propagation, where events emitted by an object such as a UIButton are handled by an observable object further up the tree — such as an Activity.
Property bindings — Whereas events typically flow ‘up’ a tree towards containing objects, data from these objects can be bound so that it makes its way ‘down’. In practice, this can be used to update properties automatically, such as the text of a UIText object when a corresponding property is set on an Activity. Refer to Binding for details.
Constructor
new ObservableObject()
Creates a new observable object.
Type members
type ObservableObject.InferObservedValue static
Infers the value type from a Binding or property key.type ObservableObject.AsyncObserver static
Type definition for an async observer callback or configuration object.type ObservableObject.Listener static
Type definition for a callback, or set of callbacks that can be passed to the ObservableObject.listen method.type ObservableObject.AttachListener static
Type definition for a callback, or set of callbacks that can be passed to the ObservableObject.attach method.interface ObservableObject.EventDelegate static
A type of object that can be used to handle events from other (attached) objects.
Static members
ObservableObject.whence() static
Returns the containing (attached) observable object of this type, for the provided object.ObservableObject.disableBindings() static
Disables bindings on all instances of this class.ObservableObject.makeRoot() static
Turns this object into a root object that cannot be attached.
Instance members
emit()
Emits an event, immediately calling all event handlers.emitChange()
Emits a change event.listen()
Adds a handler for all events emitted by this object.listenOnce()
Returns a promise for a single event with the provided name.listenAsync()
Adds a handler for all events emitted by this object, and returns an async iterable.observe()
Observes a property, a bound property from an attached parent, or an observable object.observeAsync()
Observes one or more targets asynchronously, batching or debouncing/throttling updates.attach() protected
Attaches the specified observable object to this object.isUnlinked()
Returns true if the object has been unlinked.unlink()
Unlinks this observable object.beforeUnlink() protected
A method that’s called immediately before unlinking an object, can be overridden.
