[英]How to make onBeforeAction call wait until a function call inside finishes in 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();
}
}
}
});
我需要通過調用服務器調用來驗證存儲在Session中的身份驗證令牌。 但是,當我執行此方法時,它總是會引發異常。 而且我發現原因是因為onBeforeAction調用在validateAuthToken調用返回之前被終止了。 因此self.next()不會采取任何措施。 所以我想知道如何防止onBeforeAction調用停止直到validateAuthToken返回驗證的結果然后繼續進行?
我通過等待會話變量嘗試了不同的實現,但是似乎就緒狀態從未設置為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();
}
}
}
});
編輯 :在解決了這一問題后,我想出了一個有效的示例(沒有無限循環)。 您可以使用以下代碼:
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;
}
從waitOn調用是這樣的:
Router.route("/test", {
name: "test",
action: function() {
console.log("The data is ", Util.getResponse("testWaitOn"));
},
waitOn: function() {
return Util.waitOnServer("testWaitOn");
}
})
我寫了一篇博客文章,其中有更深入的解釋,您可以在這里找到:
http://www.curtismlarson.com/blog/2015/05/04/meteor-ironrouter-waitOn-server/
您還可以從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.