【问题标题】:MeteorJS wrapasync not working inside iron routerMeteorJS wrapasync 在铁路由器中不起作用
【发布时间】:2016-05-14 00:13:56
【问题描述】:

我想检查用户是否达到了某个级别并根据该级别进行路由。但这不会等待 wrapasync 返回。如何做到这一点?

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});
    }

【问题讨论】:

标签: javascript node.js asynchronous meteor


【解决方案1】:

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;
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-07
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    相关资源
    最近更新 更多