Documentation

observe()

Observes a property, a bound property from an attached parent, or an observable object.

observe<K extends string & keyof this>(
target: K,
f: NoInfer<(this: this, value: this[K]) => void>,
): void;

observe<T extends ObservableObject>(
target: T,
f: NoInfer<(this: this, value: T) => void>,
): T;

observe<T = unknown>(
target: Binding<T>,
f: NoInfer<(this: this, value: T, bound?: boolean) => void>,
): void;

Notes

Parameters

Return value

The target parameter

Examples

class SomeWidget extends Widget {
  constructor() {
    super();

    // Observe a bound property
    this.observe(new Binding("foo"), (fooValue) => {
      // ... handle foo value, e.g. from an activity
    });

    // Observe a property (from this object)
    this.observe("someViewModel", (someViewModel) => {
      // ... handle someViewModel value, after direct updates
      //   and change events if property is an observable object
    });
  }
  // ...
}
class SomeActivity extends Activity {
  // Observe an observable object
  constructor(public readonly auth: AuthProvider) {
    super();
    this.observe(auth, (auth) => {
      // ... handle change events on the auth provider object
      //   as long as the activity and provider are not unlinked
    });
  }
}

Related