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

TJR*_*TJR 6 meteor 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});
        }
    });
Run Code Online (Sandbox Code Playgroud)

这很好用.铁路由器似乎在等待Collection的订阅,以便为客户端获取正确的文档.但是我的模板中需要的数据延迟了findOne函数.

Template.businessItemItem.rendered = function () {
    console.log(Router.current().data()); // undefined
    window.setTimeout(function(){
      console.log(Router.current().data()); // [Object]
    }, 1000);
}
Run Code Online (Sandbox Code Playgroud)

解决方案 对于遇到同样问题的每个人 只需为您的路线添加"action"方法,如下所示:

action : function () {
   if (this.ready()) this.render();
}
Run Code Online (Sandbox Code Playgroud)

有了这个方法,一切都适合我.

Pep*_*L-G 2

我不确定我是否明白您的问题,但如果我明白,您应该阅读有关控制订阅的内容,尤其是Router.onBeforeAction('loading'). 现在,您正在重新发明轮子。