IronRouter 获取布局中的数据

IronRouter get data in layout

我们的布局需要控制器中定义的当前数据,如下所示:

TripController = RouteController.extend({
    layoutTemplate: 'tripLayout',
    data: function () {
        return Trips.findOne({_id: this.params._id});
    }
});

我们的问题似乎是一场数据竞赛:

大多数时候 Template.currentData() 为空,但有时(主要是在调试时)它是我们定义的 data。 只有当我们在 tripLayout 中重新加载页面时才会出现此问题,但当我们从另一个页面进入行程布局时不会出现此问题。

请注意,我们认为 TripController 已加载,因为控制器参数已正确设置。

Template.tripLayout.onRendered(function() { // note, this is the layout template
    Iron.controller.params._id // correct id
    Template.currentData() // null
});

我们要实现的是拆分布局,其中右侧始终相同,左侧由 yield 填充(见屏幕截图)

更新 (此更新有误)

我想我发现了错误:

waitOn: function () {
    return [Meteor.subscribe('public-trips'),
        Meteor.subscribe('my-trips')];
}

一旦我删除数组(因此只有一个订阅),

data: function () {
    return Trips.find({}).fetch();
}

不再是 return 0。我会再仔细研究一下。

更新2

所以我最初的假设是错误的,只订阅一个没有帮助。这真的只是一个竞争条件。

更新3

我设法重现了它:meteorpad

在提醒中,它显示了它拥有的玩家数量。第一次是0,然后是6。但是0应该不会出现,像我们'waitOn'吧?!

我们设法解决了 'problem'(这是预期的行为,但我们找到了解决方法)! https://github.com/iron-meteor/iron-router/issues/1464#issuecomment-158738943 中的详细信息:

Alright, here's how we fixed that problem:

The first important thing is to to include the action function like so:

action: function() {
    if (this.ready()) {
       this.render();
    }
}

That worked for almost everything, but we still had some issues. In our case, it was about templates that aren't rendered in using yield, and are thus out of Iron Router's control. Switch these over to content regions (using {{yield 'contentRegionName'}}) and then explicitly render them in the action function like so:

action: function() {
    if (this.ready()) {
        this.render();
        this.render('contentRegionTemplate', {to: 'contentRegionName'});
    } 
}

You need these action functions, since the waitOn only means that the subs defined there will be added to a waitlist, but not that that waitlist will be waited on. Essentially, what happens now is that your action function is called twice, thanks to reactivity, and the templates are only rendered once the data is ready.