javascript - 一秒弹出未找到页面-Meteor-iron-router

标签 javascript meteor iron-router

我将我的应用程序升级到 Meteor 1.0 并更新了我的 router.js,因为我不能使用 .wait() anymore .但是,现在我未找到的页面只弹出一秒钟,然后才出现“真实页面”。我该如何解决这个问题?

这是我的代码:

this.route('gamePage', {
        path: '/game/:slug/',
        onBeforeAction: [function() {
            this.subscribe('singlePlayer', this.params.slug);
            var singlePlayer = this.data();
            if (singlePlayer) {
                if (singlePlayer.upgrade) {
                    this.subscribe('upgrades', this.params.slug);
                }
            }
            this.next();
        }],
        data: function() {
            return Games.findOne({slug: this.params.slug});
        },
        waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
    });

如有任何帮助,我们将不胜感激。

最佳答案

尝试改用订阅模式。

this.route('gamePage', {
    path: '/game/:slug/',
    subscriptions: function() {
        return Meteor.subscribe('singlePlayer', this.params.slug);
    },
    onBeforeAction: function() {    
        var singlePlayer = this.data();
        if (singlePlayer) {
            if (singlePlayer.upgrade) {
                this.subscribe('upgrades', this.params.slug);
            }
        }
        this.next();
    },
    data: function() {
        return Games.findOne({slug: this.params.slug});
    },
    waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
});

但是,重要的是您还包括 loading 插件以利用 loadingTemplate

Router.configure({
    loadingTemplate: 'loading' // general purpose loading template
});

// built in plugin.. surprisingly not clearly specified in current docs, but you can dive in the code for plugins. 
// https://github.com/EventedMind/iron-router/blob/devel/lib/plugins.js

Router.onBeforeAction('loading', { only: ['gamePage'] }); // only show loading for pages with subscriptions

Router.map(function() {
  this.route('gamePage',{
      //... your other options here ..
      loadingTemplate: 'gamePageLoading', // game Page dedicated loading markup.
  });
});

如果您想留在 onBeforeAction 实现中,还有 this.ready() 模式。

this.route('gamePage', {
    path: '/game/:slug/',
    onBeforeAction: [function() {
        this.subscribe('singlePlayer', this.params.slug);

        if(this.ready()) {
            var singlePlayer = this.data();
            if (singlePlayer) {
                if (singlePlayer.upgrade) {
                    this.subscribe('upgrades', this.params.slug);
                }
            }
            this.next();
        } else {
            this.render('loading');
        }

    }],
    data: function() {
        return Games.findOne({slug: this.params.slug});
    },
    waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
});

来源:https://github.com/EventedMind/iron-router/blob/devel/Guide.md#subscriptions

我认为此更改是必要的,因为 .wait 模式被视为不必要的链接并且容易出现(编码)错误。此外,在覆盖 onBeforeAction 时显式处理 .next() 现在可确保此 Hook (如果不是所有其他 Hook ,也可能是大多数)的正确计时。

关于javascript - 一秒弹出未找到页面-Meteor-iron-router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743989/

相关文章:

meteor ,WebSocket,Nginx 502错误

javascript - Meteor/Mongo - 你如何引用查找集合? (去规范化与规范化)

mongodb - 如何关闭 Meteor Mongo?

javascript - 如何在渲染时设置 Meteor 模板标题?

javascript - 快速路由器.route : 404 Error

javascript - d3.js:从父节点向子节点传递数据onclick事件

javascript - 如何将 Firebase 查询中的对象推送到 this.state

javascript - 是否有 Meteor Any route.ready 回调?为了更轻松地附加 jQuery 事件

javascript - meteor 与铁路路径

javascript - 如何安全地从任意html中提取文本内容