Observing property changes with @observable
Learn how to work with Aurelia's observable decorator to create reactive properties inside your component view models that have change callbacks.
Unlike the @watch decorator, the @observable decorator allows you to decorate properties in a component and optionally call a change callback when the value changes. It works quite similarly to the @bindable property.
By convention, the change handler is a method whose name is composed of the property_name and the literal value 'Changed'. For example, if you decorate the property color with @observable, you have to define a method named colorChanged() to be the change handler.
This is what a basic observable would look like using conventions:
import { observable } from 'aurelia';
export class Car {
@observable color = 'blue';
colorChanged(newValue, oldValue) {
// this will fire whenever the 'color' property changes
}
}When the color value is changed, the colorChanged callback will be fired. The new changed value will be the first argument, and the existing one will be the second one.
If you prefer, you can also put the @observable decorator on classes:
import { observable } from 'aurelia';
@observable('color')
@observable({ name: 'speed', callback: 'speedCallback' })
export class Car {
color = 'blue';
speed = 300;
colorChanged(newValue, oldValue) {
// this will fire whenever the 'color' property changes
}
speedCallback(newValue, oldValue) {
// this will fire whenever the 'speed' property changes
}
}Specifying a different change callback
If you do not want to use the convention, you can define the callback name for the change handler by setting the callback property of the @observable decorator:
import { observable } from 'aurelia';
export class Car {
@observable({ callback: 'myCallback' }) color = 'blue';
myCallback(newValue, oldValue) {
// this will fire whenever the 'color' property changes
}
}Last updated
Was this helpful?