Get data async before a `Page` gets rendered

What is the correct way to get data async before a Page gets rendered?

Angular2 suggests the @CanActivate decorator as far as I understand it. Sadly this is not working with Ionic2, at least not for me and for others

Apparently Ionic2 does something with the @CanActivate decorator, see
But its not documented and i can’t figure out what it does exactly.

Nevertheless this guy points out one should use Ionics View States instead anyways, due to ionics caching. His example looks like this:

  onPageWillEnter() { 
      return this._service.getComments().then(data => this.comments = data);
  }

Which looks like he is expecting Ionic to consider the returned promise, but a quick glance a Ionics sources reveals (at least I think so) that the returned value is ignored. Hence there is no guarantee that the promise gets resolved before the page gets rendered. Here is an example with onPage* and how it does not perform as needed/expected.

So I’m lost, how does one achieve this simple task?

In the first link, it was suggested to resolve the data before navigating to the page, which burdens the knowledge which data is needed for the page on the callee. This is not an option in my opinion.

*edit: added negative example

3
Leave a Reply

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

If you only navigate to that page from one location, couldn’t you simply load the data before navigating and using the NavParams to pass the data? I am using this structure for a Profile page, from a certain index page I click on a user, and then retrieve his profile before visiting his profile. You can show a nice Loading while this is happening. let self=this; this.profileSvc.getProfile(id) .then(profile => { self.nav.push(Profile, {profile:profile}); }); Now in the Profile page you can use the NavParams to initialize your page. export class Profile { profile:any; constructor(private params:NavParams) { if(params.get('profile')) { this.profile = params.get('profile');… Read more »

Jason
Guest

I’m not sure if this is the official way to do it, but I use the Loading component for situations like this one. You can find more information in Ionic API Docs. The page .ts file would look like this: import {Component} from '@angular/core'; import {Loading, NavController} from 'ionic-angular'; @Component({ templateUrl:"page1.html" }) export class Page1 { // Loading component loading : any; // Information to obtain from server comments: string = ''; constructor(nav: NavController) { this.nav = nav; } onPageWillEnter() { // Starts the process this.showLoading(); } private showLoading() { this.loading = Loading.create({ content: "Please wait..." }); // Show the… Read more »

Jason
Guest

For anyone crawling Stackoverflow about restricting page access when using Ionic 2, it looks like Ionic’s recommended lifecycle event to tap into is ionViewCanEnter.

From the docs:

ionViewCanEnter Runs before the view can enter. This can be used as a sort of “guard” in authenticated views where you need to check permissions before the view can enter.

http://ionicframework.com/docs/v2/api/navigation/NavController/