I'll try to explain as well as I can, this is my first Ionic app ever.
My goal is to create a simple dashboard with some categories fetched from a JSON file, calling the Cordova HTTP native service. When one of the categories in the dashboard is clicked, it should load another dashboard with categories based on the particular ID param I sent through the [routerLink] directive.
It works as expected, but when I tap the "back" button and I go back to the main dashboard, if I click the same category ngOnInit() method is triggered and the http service fetches the data again.
I'd like to click on one category and call the http service only the first time, if that particular category data are not fetched yet, while simply displaying that data when this category is clicked the second time.
I've tried to check for this.category_sources but probably the component is destroyed everytime I tap the "back" button and data are lost.
ngOnInit() {
this.retrieveID();
console.log("OnInit");
this.presentLoading();
this.CategoryCtrl();
}
retrieveID() {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id'];
});
}
CategoryCtrl() {
if (this.category_sources) {
console.log("Data already existing");
this.stopLoading();
} else {
this.serverService.getReviewsCategory(this.id)
.subscribe((data) => {
this.fetched = true;
this.category_sources = data['reviews'];
this.info = data['info'];
this.banner = data['banner'];
this.fetched = true;
this.stopLoading();
});
}
}
backClicked() {
console.log("Back clicked");
this.location.back();
}
async presentLoading() {
const loadingController = this.loadingController;
const loadingElement = await loadingController.create({
spinner: 'crescent',
});
return await loadingElement.present()
}
async stopLoading() {
return await this.loadingController.dismiss();
}
}