If you are using @bindable
fields, the listChanged(newValue, oldValue)
function will be triggered every time the value of the list
is updated. You can find more information in the Aurelia documentation.
@customAttribute('if')
@templateController
export class If {
constructor(viewFactory, viewSlot){
//
}
valueChanged(newValue, oldValue){
//
}
}
Another option is to utilize ObserveLocator
as explained in a blog post by an Aurelia author here:
import {ObserverLocator} from 'aurelia-binding'; // or 'aurelia-framework'
@inject(ObserverLocator)
class Foo {
constructor(observerLocator) {
this.bar = 'baz';
var subscription = this.observerLocator
.getObserver(this, 'bar')
.subscribe(this.onChange);
}
onChange(newValue, oldValue) {
alert(`bar changed from ${oldValue} to ${newValue}`);
}
}
UPDATE
In a response on this question provided by Jeremy Danyow:
The ObserverLocator is Aurelia's internal low-level API. There is now a public API for the binding engine that could be used:
import {BindingEngine} from 'aurelia-binding'; // or from 'aurelia-framework'
@inject(BindingEngine)
export class ViewModel {
constructor(bindingEngine) {
this.obj = { foo: 'bar' };
let subscription = bindingEngine.propertyObserver(this.obj, 'foo')
.subscribe((newValue, oldValue) => console.log(newValue));
subscription.dispose();
}
}
Warm regards, Alexander