我在配置路由的waitOn
部分时遇到问题,其中订阅的参数之一由来自不同订阅的文档的值确定。
游戏中的集合是候选人和面试。面试将有一个且只有一个候选人。下面是一些示例数据:
candidate = {
_id: 1
firstName: 'Some',
lastName: 'Developer'
//other props
};
interview = {
_id: 1,
candidateId: 1
//other props
};
路由配置如下。
this.route('conductInterview', {
path: '/interviews/:_id/conduct', //:_id is the interviewId
waitOn: function () {
return [
Meteor.subscribe('allUsers'),
Meteor.subscribe('singleInterview', this.params._id),
// don't know the candidateId to lookup because it's stored
// in the interview doc
Meteor.subscribe('singleCandidate', ???),
Meteor.subscribe('questions'),
Meteor.subscribe('allUsers')
];
},
data: function () {
var interview = Interviews.findOne(this.params._id);
return {
interview: interview,
candidate: Candidates.findOne(interview.candidateId);
};
}
});
问题是我没有要在waitOn
方法中传递给singleCandidate
订阅的候选人 ID,因为它存储在面试文档中。
我想到了两种可能的解决方案,但我真的不喜欢其中任何一个。首先是将路线更改为类似 /interviews/:_id/:candidateId/conduct
.第二种是将数据非规范化,并将候选人的信息存储在面试文档中。
除了这两个之外,还有其他选择可以实现这一目标吗?
通过阅读这篇关于反应式连接的文章,您可能会得到一些想法。因为您需要获取候选人作为路由数据的一部分,所以最简单的方法似乎是同时发布面试和候选人:
Meteor.publish('interviewAndCandidate', function(interviewId) {
check(interviewId, String);
var interviewCursor = Interviews.find(interviewId);
var candidateId = interviewCursor.fetch()[0].candidateId;
return [interviewCursor, Candidates.find(candidateId);];
});
但是,此联接不是反应性的。如果将不同的候选人分配到面试中,则不会更新客户。不过,我怀疑在这种情况下这不是问题。
你可以更改你的发布函数 singleCandidate 以 interviewId 作为参数而不是 candidate Id 并传递 this.params._id
有类似的问题,我设法通过订阅中的回调解决了它
http://docs.meteor.com/#/basic/Meteor-subscribe
例如,您有带有城市 ID 的用户数据,您需要获取城市对象
waitOn: ->
router = @
[
Meteor.subscribe("currentUserData", () ->
user = Meteor.user()
return unless user
cityIds = user.cityIds
router.wait( Meteor.subscribe("cities", cityIds)) if cityIds
)
]