[英]How to make onBeforeAction call wait until a function call inside finishes in meteor.js?
I have a synchronized onBeforeAction method with meteor.js 我有一个与meteor.js同步的onBeforeAction方法
Router.onBeforeAction(function() {
var self;
self = this;
authToken = Session.get('authToken');
if (!authToken) {
this.redirect('login');
this.next();
} else {
Meteor.call('validateAuthToken', authToken, function (error, result)) {
if (result) {
self.next();
} else {
self.redirect('login');
self.next();
}
}
}
});
I need to validate an authentication token stored in Session by invoking a server call. 我需要通过调用服务器调用来验证存储在Session中的身份验证令牌。 But this method always throws an exception when I am executing it. 但是,当我执行此方法时,它总是会引发异常。 And I found out the reason is because the onBeforeAction call is terminated before the validateAuthToken call returns. 而且我发现原因是因为onBeforeAction调用在validateAuthToken调用返回之前被终止了。 And thus the self.next() won't take action. 因此self.next()不会采取任何措施。 So I wonder what can I do to prevent the onBeforeAction call from stopping until the validateAuthToken returns the validated result and then proceed? 所以我想知道如何防止onBeforeAction调用停止直到validateAuthToken返回验证的结果然后继续进行?
I've tried a different implementation by wait on a session variable, but it seems the ready state is never set to true 我通过等待会话变量尝试了不同的实现,但是似乎就绪状态从未设置为true
Router.onBeforeAction(function() {
var authToken;
authToken = Session.get('authToken');
if (!authToken) {
this.redirect('login');
this.next();
} else {
Meteor.call('validateAuthToken', authToken, function (error, result) {
if (!error) {
Session.set("tokenValidated", result);
}
});
this.wait(Meteor.subscribe('token', Session.get('tokenValidated')));
if (this.ready()) {
if (!Session.get("tokenValidated")) {
this.redirect('login');
this.next();
} else {
this.next();
}
}
}
});
EDIT : After working with this problem for a little bit I came up with a working example (without the infinite loop). 编辑 :在解决了这一问题后,我想出了一个有效的示例(没有无限循环)。 You can use the following code: 您可以使用以下代码:
Util = {};
// We need to store the dep, ready flag, and data for each call
Util.d_waitOns = {};
// This function returns a handle with a reactive ready function, which
// is what waitOn expects. waitOn will complete when the reactive function
// returns true.
Util.waitOnServer = function(name) {
// This prevents the waitOnServer call from being called multiple times
// and the resulting infinite loop.
if (this.d_waitOns[name] !== undefined &&
this.d_waitOns[name].ready === true) {
return;
}
else {
this.d_waitOns[name] = {};
}
var self = this;
// We need to store the dependency and the ready flag.
this.d_waitOns[name].dep = new Deps.Dependency();
this.d_waitOns[name].ready = false;
// Perform the actual async call.
Meteor.call(name, function(err, or) {
// The call has complete, so set the ready flag, notify the reactive
// function that we are ready, and store the data.
self.d_waitOns[name].ready = true;
self.d_waitOns[name].dep.changed();
self.d_waitOns[name].data = (err || or);
});
// The reactive handle that we are returning.
var handle = {
ready: function() {
self.d_waitOns[name].dep.depend();
return self.d_waitOns[name].ready;
}
};
return handle;
}
// Retrieve the data that we stored in the async callback.
Util.getResponse = function(name) {
return this.d_waitOns[name].data;
}
Which is called from waitOn like so: 从waitOn调用是这样的:
Router.route("/test", {
name: "test",
action: function() {
console.log("The data is ", Util.getResponse("testWaitOn"));
},
waitOn: function() {
return Util.waitOnServer("testWaitOn");
}
})
I wrote a blog post with a more in depth explanation, which you can find here: 我写了一篇博客文章,其中有更深入的解释,您可以在这里找到:
http://www.curtismlarson.com/blog/2015/05/04/meteor-ironrouter-waitOn-server/ http://www.curtismlarson.com/blog/2015/05/04/meteor-ironrouter-waitOn-server/
You can also use this code snippet from https://github.com/iron-meteor/iron-router/issues/426 您还可以从https://github.com/iron-meteor/iron-router/issues/426使用此代码段
Ready = new Blaze.ReactiveVar(false);
Router.route('feed',{
waitOn: function () {
Meteor.call('getInstagramUserFeed', function(error, result) {
if(!error) Ready.set(result)
});
return [
function () { return Ready.get(); }
];
},
action: function () {
if (this.ready()) this.render('feed')
else this.render('LoadingMany');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.