attach()
Attaches the specified observable object to this object.
protected attach<T extends ObservableObject>(
target: T,
listener?: ObservableObject.AttachListener<T>,
): T;
Notes
- This method makes the current object the ‘parent’, or containing object for the target object. If the target object is already attached to another object, it’s detached from that object first.
- When using
{ delegate }, the delegate handler runs after alllisten()handlers on the target, allowing listeners to callstopPropagation()before the event bubbles up.
Parameters
- target — The object to attach
- listener — A function that will be called when the target object emits an event; or an object of type ObservableObject.AttachListener (which includes an option to delegate events)
Return value
The newly attached object
Errors
This method throws an error if the provided object was unlinked, or if a loop is detected (i.e. the target object would be indirectly attached to itself).
Examples
// Attach an object once, in the constructor
class MyObject extends ObservableObject {
foo = "bar";
}
class ParentObject extends ObservableObject {
readonly target = this.attach(
new MyObject(),
(event) => {
// ... handle event
}
)
}
Related
class ObservableObject
The base class of all observable objects, which can be placed into a tree structure to enable event handling and data binding.
