私は Ionic 2 が初めてで、それは観測可能です。次の問題があります。関数findAllを持つデータサービスがあります。private var products != false
この関数は、その値を返す必要があるかどうかをチェックします。そうでない場合は、をチェックしてnativestorage
、値が満たされている場合はその値を返す必要があります。そうでない場合は、http get をチェックしてそれを返します。
私の問題、私はfindAll
関数にサブスクライブしたくありません.http getの場合、これは可能です.observableを返すため(私が正しい場合)、nativestorage getitemはobservableではなくpromiseを返します. すでに満たされたプライベート変数の最初のシナリオも観察可能ではありません。
うまくいけば、私はこの問題を十分に明確に説明しました
@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);
}
}
}