【问题标题】:Using Iron Router to waitOn subscription that's dependent on data from a doc that will come from another subscription使用 Iron Router 来 waitOn 订阅,该订阅依赖于来自另一个订阅的文档中的数据
【发布时间】:2014-03-17 08:38:11
【问题描述】:

我在配置路由的 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 订阅的candidateId,因为它存储在面试文档中。

我想到了两种可能的解决方案,但我不太喜欢其中任何一种。首先是将路由更改为/interviews/:_id/:candidateId/conduct 之类的东西。二是将数据去规范化,将候选人的信息存储在面试文档中。

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

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

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

    【讨论】:

    • 呃……太简单了。我在这棵树上看不到森林。我在其他地方使用singleCandidate 发布,但我将添加interviewCandidate 发布。谢谢!
    【解决方案2】:

    阅读this post 关于反应式连接,您可能会得到一些想法。因为您需要获取候选人作为路线数据的一部分,所以似乎最简单的方法就是同时发布面试和候选人:

    Meteor.publish('interviewAndCandidate', function(interviewId) {
      check(interviewId, String);
    
      var interviewCursor = Interviews.find(interviewId);
      var candidateId = interviewCursor.fetch()[0].candidateId;
    
      return [interviewCursor, Candidates.find(candidateId);];
    });
    

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

    【讨论】:

    • 谢谢。这也是一个很好的解决方案。
    • 订阅会是什么样子?
    • @JoeC Meteor.subscribe('interviewAndCandidate')
    【解决方案3】:

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

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

    【讨论】:

    • 太棒了!回调是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2023-03-20
    • 2018-07-12
    相关资源
    最近更新 更多