I’m new to Ionic 2 and it’s observables. I have the following problem. I have a dataservice which has a function findAll. This function checks if the private var products != false if so it needs to return that value. if not, it needs to check the nativestorage and return that value if it is filled, if not, it checks http get and returns that
My problem, I wan’t to subscribe to the findAll function, in case of the http get this is possible, because it returns an observable (if i’m right), the nativestorage getitem returns a promise not an observable. and the first scenario of the already filled private variable is also not an observable.
Hopefully I described this problem clear enough
@Injectable()
export class dataService {
products = false;
constructor (http:Http) {
this.http = http;
}
findAll() {
if (this.products !== false) {
//HOWTO return this.products AS OBSERVABLE?
} else {
//HOWTO check native storage and return observable??
NativeStorage.getItem('products')
//ELSE RETURN observable http get and
//HOWTO ADD TO this.products and NativeStorage.setItem
return this.http.get(productsURL)
.map(res => res.json())
.catch(this.handleError);
}
}
}