Let's say I have an array of elements and in addition to displaying the list in my app, I want to sync the list to the server with HttpClient
. How can I observe changes to the array? I tried:
@inject(ObserverLocator)
export class ViewModel {
constructor(obsLoc) {
this.list = [];
obsLoc.getObserver(this, 'list');
.subscribe(li => console.log(li));
}
}
But I got neither error nor log message.
getObserver
returns a property observer which will notify you when theViewModel
class instance'slist
property changes. This will only happen when you assign a new value to thelist
property, iethis.list = [1,2,3]
. If you're not assigning new values to thelist
property and instead are mutating the value of the property viapush
,pop
,splice
, etc, you'll want to use an array observer. Use theObserverLocator
'sgetArrayObserver
method- it takes one parameter, the array you want to observe:October 2015 update
The ObserverLocator is Aurelia's internal "bare metal" API. There's now a public API for the binding engine that could be used: