I have a method in my data service which selects records from SqlStorage and returns the promise object…
import {Storage, SqlStorage} from 'ionic-angular'; import {Injectable} from 'angular2/core'; import {Journey} from '../../models/journey-model';
@Injectable() export class Data { db: any;
constructor() { this.db = new Storage(SqlStorage, { name: 'journeyData.js' }); }
getJourneys() { let queryString = 'SELECT * FROM journey ORDER BY endTime DESC'; return this.db.query(queryString); } }
And I have a page which uses the data service…
import {Page, NavController} from 'ionic-angular'; import {Data} from '../../providers/data/data';
@Page({ templateUrl: 'build/pages/journey-history/journey-history.html', }) export class JourneyHistoryPage { isLoading: boolean; journeyList = []; constructor(public nav: NavController, private dataService: Data) { this.nav = nav; this.dataService = dataService; }
getJourneys() { this.isLoading = true; this.dataService.getJourneys().then((response) => { if (response.res.rows.length > 0) { for (let journey of response.res.rows) this.journeyList.push(journey); } this.isLoading = false; }, (err) => { this.isLoading = false; }); }
refreshList(refresher) { setTimeout(() => { refresher.complete(); }, 1000); }
onPageWillEnter() { this.getJourneys(); } }
And this is the page view
<ion-navbar *navbar teale> <ion-title>Journey App</ion-title> </ion-navbar> <ion-content padding class="app-background"> <div *ngIf="isLoading" class="text-center"> <ion-spinner name="circles"></ion-spinner> </div> <div *ngIf="!isLoading"> Display list... </div> </ion-content>
Steps that happen are:
-
On page enter
getJourneys()
is called. -
In here the isLoading variable is set to true and the
ion-spinner
is displayed. -
Data is then fetched from the data service and returned as a promise.
-
Returned objects are pushed into a list and the isLoading variable is set to false.
my problem is that when I loop through the data in the promise the this
keyword is undefined but it should still be the JourneyHistoryPage am I right? so the variables on the page won’t update and the ion-spinner
continues to display.
Can anybody tell me if I have done something wrong here?