9

我在配置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);
        };
    }
});

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

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

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

4

3 回答 3

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

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

于 2014-02-17T22:25:28.927 回答
2

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

于 2014-02-17T22:15:01.763 回答
2

我有类似的问题,我设法通过订阅中的回调解决了它

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        
        )
    ]
于 2015-01-16T09:37:16.137 回答