Observation
Aurelia’s observation system is a core feature that enables seamless data binding between your application’s model and view. This document provides a comprehensive guide to Aurelia's observation mechanisms, including the Observer Locator, the @observable decorator, and other strategies for observing changes in your application state.
Aurelia’s observation system is designed to efficiently detect changes in your application’s data models and automatically update the views. This two-way data binding ensures the user interface remains in sync with the underlying data without manual intervention.
Key features of the observation system include:
• Automatic Change Detection: Monitors changes to properties and collections.
• Efficient Updates: Updates the UI only when necessary, improving performance.
• Extensibility: Allows for custom observation strategies.
Observer Locator
The Observer Locator is a service in Aurelia that determines the best way to observe a property or collection for changes.
The Observer Locator is responsible for:
• Identifying the appropriate observer for a given property or object.
• Providing low-level observation APIs.
• Managing the lifecycle of observers.
It acts as a central hub that other parts of the Aurelia framework use to subscribe to changes.
Using the observer locator
While Aurelia’s binding system typically handles observation automatically, you can use the Observer Locator directly when you need fine-grained control.
Injecting the Observer Locator
Observing a property
Observing an Array
The @observable Decorator
The @observable decorator provides a convenient way to make class properties observable, enabling Aurelia to monitor changes and update the view accordingly.
Understanding the @observable Decorator
When you apply the @observable decorator to a property, Aurelia:
• Generates a property with a getter and setter that notifies Aurelia’s binding system upon changes.
• Creates a corresponding change handler method if you follow the naming convention (propertyNameChanged).
• Allows for two-way data binding between the property and the view.
Using @observable in your components
Importing the Decorator
Applying the Decorator
Setting an Initial Value
Example usage
Explanation:
• The count property is decorated with @observable, making it observable by Aurelia.
• The countChanged method is automatically called whenever count changes.
• The view displays the current value of count and provides buttons to modify it.
• The UI updates automatically when count changes, thanks to Aurelia’s data binding.
Collection Observation
For collections like arrays, maps, and sets, Aurelia observes mutations to keep the UI in sync.
Array Observation
• Observes methods like push, pop, splice, shift, unshift, and reverse.
• Automatically updates bindings when the array changes.
Computed Observation
Aurelia can observe computed properties that depend on other observable properties using the @computedFrom decorator.
Explanation
• The fullName getter is decorated with @computedFrom, specifying its dependencies.
• Aurelia only recalculates fullName when firstName or lastName changes.
• Improves performance by avoiding unnecessary recalculations and updates.
Custom Observers
Creating custom observers allows you to define how Aurelia observes changes in non-standard objects or properties.
Implementing a Custom Observer:
• Create a class that implements the PropertyObserver interface.
• Implement the required methods: getValue, setValue, subscribe, and unsubscribe.
Last updated
Was this helpful?