gpt4 book ai didi

meteor - Iron-router 在渲染之前等待 Collection.findOne() 作为数据对象

转载 作者:行者123 更新时间:2023-12-04 15:44:21 25 4
gpt4 key购买 nike

我正在寻找一种解决方案,即 Iron-router 在渲染之前等待我的集合上的成功查找方法。

我的路线是这样的:

this.route('business', {
path : '/business/:type/:_id',
waitOn : function () {
return Meteor.subscribe('business', this.params._id);
},
data : function () {
return Business.findOne({_id: this.params._id});
}
});

这工作正常。似乎 Iron-router 正在等待 Collection 的订阅来为客户端取回正确的 Document。但是我在模板中需要的数据对于 findOne 函数有延迟。
Template.businessItemItem.rendered = function () {
console.log(Router.current().data()); // undefined
window.setTimeout(function(){
console.log(Router.current().data()); // [Object]
}, 1000);
}

解决方案
对于有同样问题的每个人。只需为您的路线添加“ Action ”方法,如下所示:
action : function () {
if (this.ready()) this.render();
}

使用这种方法,一切对我来说都很好。

最佳答案

我不确定我是否遇到了您的问题,但是如果我遇到了,您应该阅读 Controlling subscriptions ,尤其是 Router.onBeforeAction('loading') .现在,您正在重新发明轮子。

关于meteor - Iron-router 在渲染之前等待 Collection.findOne() 作为数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com