class Observer
The base class for an observer that watches a particular ManagedObject.
class Observer<T extends ManagedObject = ManagedObject>
Description
Observers can be used to watch particular objects (instances of ManagedObject or any subclass, including ManagedList, ManagedRecord, etc.), specifically to:
- Listen for events (see handleEvent(), which can be overridden or used to dispatch events to specific Observer methods).
- Check when the object is attached (see handleAttachedChange(), which can be overridden).
- Check when the object is unlinked (see handleUnlink(), which can be overridden).
- Handle property changes (see observeProperty() and observePropertyAsync() which must be called to observe a property).
Before any property can be observed, the observeProperty*()
method must be called. This should be done within the observe() method, which must be overridden. Refer to the examples below.
Observers can be created manually (using new
, and the observe() method), but they can also be passed to ManagedObject.attach() and ManagedObject.autoAttach() to observe attached objects. Refer to the examples below.
The observer is automatically stopped when the target is unlinked, but it can also be stopped manually (see stop()). This may be necessary to avoid memory leaks, since observers are linked to the observed object while they’re active — keeping them (and any referenced objects) from being freed up by the JavaScript garbage collector as long as the observed object is referenced itself.
Constructor
- constructor()Creates a new observer, without observing any object yet.
Instance Members
- observed readonlyThe currently observed object, if any.
- observe(observed)Starts observing the specified managed object.
- stop()Stops observing events and properties.
- observeProperty(…properties) protectedStarts observing one or more properties.
- observePropertyAsync(…properties) protectedStarts observing one or more properties, asynchronously.
- handleEvent(event) protectedA method that’s called when an event is emitted on the observed object, may be overridden.
- handlePropertyChange(property, value, event?) protectedA method that’s called when an observed property has been changed on the observed object, may be overridden.
- handleAttachedChange(origin) protectedA method that’s called when the observed object has been attached to another object, may be overridden.
- handleUnlink() protectedA method that’s called when the observed object is unlinked, may be overridden.