使用Iron Router进行waitOn订阅,该订阅取决于来自另一个订阅的文档中的数据 [英] Using Iron Router to waitOn subscription that's dependent on data from a doc that will come from another subscription

查看:58
本文介绍了使用Iron Router进行waitOn订阅,该订阅取决于来自另一个订阅的文档中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble configuring the waitOn portion of a route where one of the subscription's parameters is determined by the value from a doc that comes from a different subscription.

正在播放的收藏是候选人和访谈.面试只有一名候选人.以下是一些示例数据:

The collections in play are Candidates and Interviews. An interview will have one and only one candidate. Here's some sample data:

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);
        };
    }
});

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

The problem is that I don't have a candidateId to pass to the singleCandidate subscription in the waitOn method because it's stored in the interview doc.

我已经想到了两种可能的解决方案,但我真的不喜欢它们中的任何一种.首先是将路线更改为/interviews/:_id/:candidateId/conduct之类的东西.第二个方法是对数据进行非规范化,并将应聘者的信息存储在面试文档中.

I've thought of two possible solutions, but I don't really like either of them. The first is to change the route to something like /interviews/:_id/:candidateId/conduct. The second is to denormalize the data and store the candidate's info in the interview doc.

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

Are there any other options to accomplish this besides those two?

推荐答案

您可以更改发布函数singleCandidate以将受访者ID作为参数而不是候选人ID并传递this.params._id

You can change your publish function singleCandidate to take interviewId as paramater instead of candidateId and pass this.params._id

这篇关于使用Iron Router进行waitOn订阅,该订阅取决于来自另一个订阅的文档中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆