简体   繁体   中英

Using ngrx in ionic guard

In ionic I have guard. In my old version it looked like this (I'm getting data from server):

ionViewCanEnter(): Promise<any> {
    return this.miejscaService.pobierzSzczegoly(this.navParams.data)
         .do(resp => {
            this.atrakcja = resp;
        })
        .toPromise();
}

It worked perfectly but I want to cache data so I'm using now ngrx store. I get data but guard is not working:/ No error. Have no idea why...

ionViewCanEnter(): Promise<any> {
    return this.store.select(getWybranaAtrakcja)
        .do(resp => {
            console.log('I\'m here');
            console.log(resp);
            this.atrakcja = resp;
        })
        .toPromise();
}

As I googled there is small bug (in my opinion). Reaching to store was never resolved (in terms of promise). https://github.com/Reactive-Extensions/RxJS/issues/1088

ionViewCanEnter(): Promise<boolean> {
    let resolver = (subsription: Subscription, resolve: Function, result: any) => {
        resolve(result);
        subsription.unsubscribe();
    };
    let rejecter = (subsription: Subscription, reject: Function, error: any) => {
        reject(error);
        subsription.unsubscribe();
    };

    return new Promise((resolve, reject) => {
        let subscription = this.store.select(getWybranaAtrakcja)
            .pipe(
                filter(resp => !!(resp))
            )
            .subscribe(
                result => {
                    this.atrakcja = result;
                    resolver(subscription, resolve, result);
                },
                error => {
                    rejecter(subscription, reject, error);
                }
            );
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM