检测流星中的浏览器刷新

时间:2015-05-29 12:43:03

标签: javascript meteor iron-router browser-refresh

我有一个名为&#34的容器模板;问卷"单击下一个按钮,一个接一个地显示问题。当单击下一个按钮时,后台有一些逻辑将为我提供下一个问题的路径。我只会在调查问卷中更改问题模板。

<template name="questionnaire">
    <h1>Snapwiz questionnaire</h1>
    <h3>Question</h3>
    {{> yield region="singleQuestion"}}
    <button class="next">Next</button>
</template>

Template.questionnaire.events({
    'click .next':function(event,template){ 
           Meteor.call('nextQuestion',Router.current().params._id,function(err,res){
            if(res){
                Router.go('/question/'+res);
            }
        });
    }
});

Router.route('questionnaire',{
    yieldTemplates:{
        'question1':{to:'singleQuestion'}
    }
});

Router.route('question',{
    path:'question/:_id',
    action:function(){
        this.render('question'+this.params._id,{to:'singleQuestion'});
    }
});

有许多简单的问题模板,例如&#34;问题1&#34;,&#34;问题2&#34;等,只需点击下一个按钮即可显示。

当我们第一次点击链接&#34; http://localhost:4000/questionnaire&#34;。 &#34;问题1&#34;默认显示模板。现在,当用户点击&#34; next&#34;按钮,我们从服务器获取问题ID并显示该问题模板。所以点击下一个按钮会让我们看到&#34; http://localhost:4000/question/2&#34;,向我们展示问题2模板。

到目前为止一切顺利。

但问题出现在我们放置&#34; http://localhost:4000/question/2&#34;直接在浏览器中重新加载。由于未加载容器,页面将显示为空白。

那么最终解决这个问题的方法是什么?我觉得如果我们检测到热推码&#34;在路由器中,我们也可以渲染容器模板,但我们应该如何做呢?我不确定哪种方法最好。

任何想法或帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

action回调中,您从未告诉Iron Router使用questionnaire作为布局模板。要解决此问题,您只需致电this.layout

action: function () {
    this.layout('questionnaire');
    this.render('question' + this.params._id, {to: 'singleQuestion'});
}
相关问题