2016-02-04 50 views
0

我想檢查用戶是否已經達到了一定的水平,並根據這個做路由。但是這不等待wrapasync返回。如何實現這一目標?MeteorJS包裹同步不工作鐵路由器

Router.js

var getLevelAsync = function (userID, callback) { 
    Meteor.call('getLevel', Meteor.userId(), function (err, data) { 
     callback(null, data['level']); 
    }) 
} 

var getLevel = Meteor.wrapAsync(getLevelAsync); 


Router.route('/verifyData', function(){ 
    var level = getLevel(Meteor.userId()); 
    if(level < 3) this.render('somepage'); 
    else this.render('another page'); 

}) 

服務器/ methods.js

getLevel: function (userID) { 
     return Meteor.users.findOne({_id: userID}); 
    } 
+0

WrapAsync不會在客戶端上運行。您可以使用回調,承諾或反應計算,但不能使用'wrapAsync'提供的僞同步行爲。 – MasterAM

+0

[如何在客戶端上使用Meteor.wrapAsync?](http://stackoverflow.com/questions/29478707/how-to-use-meteor-wrapasync-on-the-client) – MasterAM

回答

0

Meteor.wrapAsync周圍的功能,只能在服務器上進行包裝了一個未來。你不需要Meteor方法來實現你想要做的事情,你可以簡單地渲染同一頁面,但是根據當前用戶渲染不同的上下文。

{{#with currentUser}} 
    {{#if aboveLevelThree}} 
    <h1>Congrats! You're in the special club!</h1> 
    {{else}} 
    <h3>Sorry mate, get a better score to advance</h3> 
    {{/if}} 
{{/with}} 

如果你的模板助手是這樣的:

Template['someTemplate'].helpers({ 
    aboveLevelThree: function() { 
    return this.level > 3; 
    } 
});