Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

meteor - Using Iron Router to waitOn subscription that's dependent on data from a doc that will come from another subscription

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

The route is configured as follows.

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

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may get some ideas by reading this post on reactive joins. Because you need to fetch the candidate as part of the route's data, it seems like the easiest way is just to publish both the interview and the candidate at the same time:

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

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

  return [interviewCursor, Candidates.find(candidateId);];
});

However, this join is not reactive. If a different candidate gets assigned to the interview, the client will not be updated. I suspect that isn't a problem in this case though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...