简体   繁体   中英

Wait for page data in iron router - Meteor

Is it possible to wait until the page data is loaded before page rendering? I always see the notFound template for a few milliseconds before the data is loaded.

Here is my code:

this.route('gamePage', {
        path: '/game/:slug/',
        onBeforeAction: filter,
        waitOn: function() { return [Meteor.subscribe('game', this.params.slug)]; },
        data: function() {
            var game =  Games.findOne({slug: this.params.slug});
            if (!game) {
                this.render("notFound");
            } else {
              return game;
            }
        }
    });

Any help would be greatly appreciated.

You can use the 'loading' hook to display a template of your choice while the subscriptions in waitOn are not yet ready.

Activate the hook:

Router.onBeforeAction("loading");

And set a loading template:

Router.configure({
  loadingTemplate: "loading"
});
<template name="loading">
  loading... <!-- or display an animated spinner -->
</template>

You can also set the loading template on a per-route level.

this.route("blah", {
  path: "/blah",
  loadingTemplate: "blahLoading"
});

Here is how I solved it:

if (!game && this.ready()) {
       this.render("notFound");
 } else {
       return game;
 }

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