July 2018
Beginner
236 pages
5h 34m
English
Let's take an example where we will lazy load the temperature for a city, but only when it is accessed. This can be done by modeling the observable property with the hooks for onBecomeObserved() and onBecomeUnobserved(). The following snippet shows this in action:
// A mock service to simulate a network call to a weather APIconst temperatureService = { fetch(location) { console.log('Invoked temperature-fetch'); return new Promise(resolve => setTimeout(resolve(Math.round(Math.random() * 35)), 200), ); },};class City { @observable temperature; @observable location; interval; disposers; constructor(location) { this.location = location; const disposer1 = onBecomeObserved( this, 'temperature', this.onActivated, ); ...
Read now
Unlock full access