Docs (4.0.0)
class ManagedObject method

autoAttach

Observes a property, so that any object assigned to it is attached immediately.

protected

protected autoAttach<K extends keyof this, T extends NonNullable<this[K]> & ManagedObject>(propertyName: K, observer?: Observer<T> | ManagedObject.AttachObserverFunction<T>): void

Notes

  • This method makes the current object the ‘parent’, or containing object of all objects assigned to the property.
  • Refer to ManagedObject for more information on attaching objects.

Parameters

  • propertyName — The name of the property to watch for references to other managed objects; must be a public property
  • 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)

Example

// Attach any object assigned to a property
class MyObject extends ManagedObject {
  foo = "bar";
}
class ParentObject extends ManagedObject {
  constructor() {
    super();
    this.autoAttach(
      "target",
      (target, event) => {
        // ...target is either the target object, or undefined
        // ...event is a change event, or undefined
      }
    );
  }
  target?: MyObject;
}

let parent = new ParentObject();
parent.target = new MyObject(); // attached right away
parent.target.unlink();
parent.target // => 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.