使用Iron Router进行waitOn订阅,该订阅依赖于来自其他订阅的文档的数据

Cra*_*g M 9 meteor iron-router

我在配置waitOn路由的一部分时遇到问题,其中一个订阅的参数由来自不同订阅的文档中的值确定.

游戏中的收藏品是候选人和访谈.面试将只有一名候选人.这是一些示例数据:

candidate = {
    _id: 1
    firstName: 'Some',
    lastName: 'Developer'
    //other props
};

interview = { 
    _id: 1,
    candidateId: 1
    //other props
};
Run Code Online (Sandbox Code Playgroud)

路由配置如下.

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);
        };
    }
});
Run Code Online (Sandbox Code Playgroud)

问题是我没有候选ID传递给方法中的singleCandidate订阅,waitOn因为它存储在面试文档中.

我想到了两种可能的解决方案,但我真的不喜欢它们中的任何一种.首先是将路线改为类似的路线/interviews/:_id/:candidateId/conduct.第二种是对数据进行非规范化并将候选人的信息存储在访谈文档中.

除了这两个之外,还有其他选择吗?

Dav*_*don 5

您可以通过阅读有关反应性连接的帖子获得一些想法.因为您需要将候选人作为路线数据的一部分来获取,所以最简单的方法似乎就是同时发布面试和候选人:

Meteor.publish('interviewAndCandidate', function(interviewId) {
  check(interviewId, String);

  var interviewCursor = Interviews.find(interviewId);
  var candidateId = interviewCursor.fetch()[0].candidateId;

  return [interviewCursor, Candidates.find(candidateId);];
});
Run Code Online (Sandbox Code Playgroud)

但是,此连接不是被动的.如果将不同的候选人分配给面试,则不会更新客户.我怀疑在这种情况下这不是问题.


Ser*_*soy 2

您可以更改发布函数 singleCandidate 以将 InterviewId 作为参数而不是 CandidateId 并传递 this.params._id