Docs (4.0.0)
class ManagedObject method

attach

Attaches the provided managed object to this object.

protected

protected attach<T extends ManagedObject>(target: T, observer?: Observer<T> | ManagedObject.AttachObserverFunction<T>): T

Notes

  • This method makes the current object the ‘parent’, or containing object for the target object; not the other way around.
  • Refer to ManagedObject for more information on attaching objects.

Parameters

  • target — The object to attach
  • observer — An Observer instance, or a function that’s called whenever a change event is emitted by the target object (with target and event arguments, respectively), and when the object is unlinked or moved to another object by re-attaching it (without any arguments)

Return value

The newly attached object

Errors

  • This method throws an error if the provided object was already attached to this object, or if a circular reference was found.

Example

// Attach an object once, in the constructor
class MyObject extends ManagedObject {
  foo = "bar";
}
class ParentObject extends ManagedObject {
  readonly target = this.attach(
    new MyObject(),
    (target, event) => {
      // ...target is either the target object, or undefined
      // ...event is a change event, or undefined
    }
  )
}

Related

  • class ManagedObjectThe base class of all managed objects, which can be placed into a tree structure to enable event handling and data binding.