class ManagedObject
The base class of all managed objects, which can be placed into a tree structure to enable event handling and data binding.
class ManagedObject
Description
The ManagedObject class is the core construct that powers framework concepts such as event handling and property binding. Many of the other classes are derived from ManagedObject, including ManagedList and ManagedRecord. 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 — Managed 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 managed 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 ad-hoc using the attach() method. An Observer or callback function can be used to listen for events (see below) or wait for the object to be unlinked.
- Objects can be attached by referencing them from specific observed properties. The property needs to be watched using the autoAttach() method; afterwards, any object assigned to this property is attached automatically, and then when the referenced object is unlinked, the property is set to undefined. The autoAttach() method also accepts an observer or callback function.
- When an object is unlinked, its attached objects are unlinked as well.
Events — The emit() method ‘emits’ an event from a managed object. Events are instances of ManagedEvent, which can be handled using a callback provided to the listen() method, or an Observer that’s currently observing the object. Typically, objects are observed 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 a managed object further up the tree — such as an Activity. Refer to ManagedEvent for details.
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 such as the text of a UILabel object automatically when a particular property value is set on a containing object such as the Activity. Refer to Binding for details.
Example
// Attach an object to a parent object, handle events
class MyObject extends ManagedObject {
doSomething() { this.emit("Foo") }
}
class MyParentObject extends ManagedObject {
readonly object = this.attach(
new MyObject(),
class extends Observer<MyObject> {
onFoo(e: ManagedEvent) {
// ...handle Foo event from object
}
}
);
}
let parent = new MyParentObject();
parent.object.doSomething()
Constructor
- constructor()Creates a new managed object.
Type Members
- type ManagedObject.Constructor staticType definition for a subclass (constructor) of the ManagedObject class.
- type ManagedObject.AttachObserverFunction staticType definition for the callback function argument passed to ManagedObject.attach() and ManagedObject.autoAttach().
Static Members
- ManagedObject.whence(this, object?) staticReturns the containing (attached) managed object of this type, for the provided object.
Instance Members
- emit(event)Emits an event, immediately calling all event handlers.
- emitChange(name?, data?)Emits a change event, an instance of ManagedChangeEvent.
- listen(handler)Adds a handler for all events emitted by this object.
- isUnlinked()Returns true if the object has been unlinked.
- unlink()Unlinks this managed object.
- beforeUnlink() protectedA method that’s called immediately before unlinking an object, can be overridden.
- attach(target, observer?) protectedAttaches the provided managed object to this object.
- autoAttach(propertyName, observer?) protectedObserves a property, so that any object assigned to it is attached immediately.