2015-05-10 77 views
0

當我點擊下面觸發的/login路由時,正確的路由呈現,但頁面頂部以我的template開頭,而不是我的layoutTemplate開頭。我必須向上滾動才能看到路由中配置的layoutTemplate流星的Router.go()頁面渲染

if(! Meteor.userId()){ 
     Session.set('single_post_not_logged_in', Session.get('post_id')); 
     Router.go('/login'); 
    } 

這是獲取triggerd路線:

Router.route('login', { 
    path: '/login', 
    layoutTemplate: 'loginLayoutTemplate', 
    template: 'login_template', 
    onBeforeAction: function(){ 
     if(Meteor.loggingIn()){ 
      var post_visited = Session.get('single_post_not_logged_in'); 
      console.log(post_visited); 
      if(post_visited != undefined){ 
       Router.go('/post/' + post_visited); 
      } 

     } 
     this.next(); 
    } 
}); 

最後,這裏是路由離開佈局模板(layoutTemplate)和新的佈局模板(loginLayoutTemplate

<body> 
    {{renderPage}} 
</body> 

<template name = 'layoutTemplate'> 
    {{> loginButtons}} 
    <a id = 'home_button' href="{{pathFor 'home'}}"><h1 id='layout_header'>snippetExchange</h1></a> 
    <div id='layout_header_row_2'> 
     <h2 id='layout_by_line'>valuable answers</h2> 
     <a id = 'new_post_button' href="{{pathFor 'new_post'}}">new post</a> 
    </div> 
    {{> yield}} 
</template> 

<template name = 'loginLayoutTemplate'> 
    <a id = 'home_button' href="{{pathFor 'home'}}"><h1 id='layout_header'>snippetExchange</h1></a> 
    <div id=''> 
     <h2 id='layout_by_line'>valuable answers</h2> 
    </div> 
    {{> yield}} 
</template> 

<template name = 'login_template'> 
    <h3>please login or create an account</h3> 
    {{> loginButtons}} 
</template> 
+1

'template:login_template'但你的HTML中沒有'login_template'?檢查檢查員是否有錯誤。 'template'將replate在由'layoutTemplate'定義的模板中的'{{> yield}}' –

+0

我確實有一個'login_template',我把它包含在我的帖子的底部 – redress

+0

奇怪,今天顯示出來,昨天可見。現在我重新讀了你的問題,實際上看起來路線正在被正確渲染,但垂直滾動位置被設置爲「模板」的頂部,這是否正確?這很容易解決與模板助手和一些JavaScript,但更深的問題,爲什麼逃避我。 –

回答

0

Try:

Template.login_template.onRendered(function(){ 
    window.scrollTo(0, 0); 
}); 
+0

嗯。我多次發出警告,希望模板的渲染次數超過預期。這可能會導致一些意外的頁面滾動? –

+1

助手將被調用很多次,但給定的模板只能渲染一次。 *除非它嵌套在一個'{{#each}}'! –