2015-01-02 58 views
2

我在Meteor JS應用程序中爲我的鐵路由器signOut路由下面的代碼。我試圖將不推薦使用的Router.map轉換爲新的Router.route語法,但難以使onBeforeAction和onAfterAction正常工作。以下代碼塊的適當Router.route語法是什麼?鐵路由器的Router.route的正確語法?

Router.map(function() { 

    // sign-out the user then redirect them to the home page 
    this.route('signOut', { 
    path: '/sign-out', 
    onBeforeAction: function() { 
     if (Meteor.userId()) { 
     Meteor.logout() 
     } 
     this.next(); 
    }, 
    onAfterAction: function() { 
     this.redirect('/'); 
    } 
    }); 

}); 

回答

2
Router.route('/sign-out', function() { 
//here you put things you wanna render, it's empty since you just want to logout and redirect 
}, { 
    name: 'signOut', 
    onBeforeAction: function() { 
     if (Meteor.userId()) { 
     Meteor.logout() 
     } 
     this.next(); 
    }, 
    onAfterAction: function() { 
     Router.go('/'); 
    } 
}); 

而且我覺得你應該先加waitOn功能,原因有可能是沒有Meteor.user()對象,如果沒有預訂較早