2014-02-17 36 views
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); 
     }; 
    } 
}); 

的問題是,我沒有candidateId傳遞給singleCandidate認購的waitOn方法,因爲它存儲在採訪文檔。

我想到了兩種可能的解決方案,但我並不十分喜歡他們中的任何一種。首先是將路線改爲/interviews/:_id/:candidateId/conduct之類的路線。第二種是將數據非規範化並將候選人的信息存儲在訪談文檔中。

除了這兩個之外,還有其他的選擇可以實現嗎?

回答

2

你可以改變你的發佈功能singleCandidate採取interviewId作爲paramater代替candidateId並通過this.params._id

+0

咄...很簡單。我看不到這片樹上的樹林。我在其他地方使用'singleCandidate'出版物,但我會添加一個'interviewCandidate'出版物。謝謝! –

5

您可以通過在反應加入閱讀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);]; 
}); 

然而,這種連接是不是反應。如果不同的候選人被分配到面試中,客戶端將不會被更新。儘管如此,我懷疑這不是問題。

+0

謝謝。這也是一個很好的解決方案。 –

+0

訂閱會是什麼樣子? –

+0

@JoeC'Meteor.subscribe('interviewAndCandidate')' –

2

我有類似的問題,我成功地通過回調來解決它的訂閱

http://docs.meteor.com/#/basic/Meteor-subscribe

例如你有一個城市IDS的用戶數據,你需要得到城市對象

waitOn: -> 
    router = @ 
    [ 
     Meteor.subscribe("currentUserData",() -> 
      user = Meteor.user() 
      return unless user 
      cityIds = user.cityIds 
      router.wait(Meteor.subscribe("cities", cityIds)) if cityIds   
     ) 
    ] 
+0

酷!這對我很好。 –

+0

太棒了!回調是我需要的。 – torayeff