Documentation

class ObservableList

A data structure that contains an ordered set of observable objects.

class ObservableList<
T extends ObservableObject = ObservableObject,
> extends ObservableObject;

Description

Lists of observable objects — instances of ObservableObject — can be represented using regular arrays. However, often a ‘list of objects’ needs to be more restrictive, especially in the context of a UI framework. An observable list is useful in the following cases:

Observable lists are used in many places throughout the framework, such as to reference view objects within a UIContainer object. Observable lists can also be used by application code to efficiently represent lists of data models or other records.

Attaching list items — By default, objects aren’t attached to the list, so they could be attached to other objects themselves and still be part of an observable list. However, you can enable automatic attachment of objects in a list using the ObservableList.attachItems() method. This ensures that objects in a list are only attached to the list itself, and are unlinked when they’re removed from the list (and removed from the list when they’re no longer attached to it, e.g. when moved to another auto-attaching list).

Events — Since ObservableList itself inherits from ObservableObject, a list can emit events, too. When any objects are added, moved, or removed, a change event is emitted. The event names are either Change, Add, or Remove, with objects referenced by added or removed event data properties if possible. In addition, for auto-attaching lists (see ObservableList.attachItems()) any events that are emitted by an object are re-emitted on the list itself. The ObservableEvent.source property can be used to find the object that originally emitted the event.

Nested lists — Observable lists can contain (and even attach to) other observable lists, which allows for building nested or recursive data structures that fully support bindings and events.

Examples

// Create a list, add and remove objects
let a = Object.assign(new ObservableObject(), { foo: "a" })
let b = Object.assign(new ObservableObject(), { foo: "b" })
let list = new ObservableList(a, b);

let c = Object.assign(new ObservableObject(), { foo: "c" })
list.add(c);
list.length // => 3

list.remove(b);
list.length // => 2

let a = list.map(record => record.foo);
a // => ["a", "c"]

// Lists can be iterated using for...of, too
for (let item of list) {
  // ...do something for each item
}

Constructor

Instance members

Inherited members