2014-09-20 70 views
0

我想根據文檔中的布爾屬性(mode)來決定哪些用戶訂閱,但我在設計方法方面存在問題。如果我在waitOn函數中使用this.data()和if子句,則將呈現404頁面,因爲我需要首先訂閱。鐵路由器在waitOn函數中選擇不同的訂閱 - 流星

這裏是我的代碼:

this.route('gamePage', { 
     path: '/game/:slug/viewGame/:produceDate?/:releaseDate?', 
     onBeforeAction: [memberFilter], 
     waitOn: function() { 
      var game = this.data(); 
      if (game.mode) { 
       if (this.params.produceDate && this.params.releaseDate) { 
        return [Meteor.subscribe('singleGame', this.params.slug), 
         Meteor.subscribe('authorsMode', this.params.slug, this.params.produceDate, this.params.releaseDate)]; 
       } else { 
        return [Meteor.subscribe('singleGame', this.params.slug), Meteor.subscribe('console', this.params.slug)]; 
       } 
      } else { 
       return [Meteor.subscribe('singleGame', this.params.slug), 
        Meteor.subscribe('authors', this.params.slug)]; 

      } 
     }, 
     data: function() { 
      return Games.findOne({slug: this.params.slug}); 
     } 

任何幫助將不勝感激。

回答

1

要直接實現這一目標,您需要Iron Router來讓您擁有2級訂閱,第一個數據可用於下一級訂閱。此功能目前不存在。我剛剛向IR回購提交了一個issue,要求提供此功能。

這種情況的解決辦法是有不同的URL爲每個個案,使中間重定向:

this.route('gamePage', { 
    path: '/game/:slug/viewGame/:produceDate?/:releaseDate?', 
    onBeforeAction: function() { 
    var game = this.data(); 
    var params = { 
     slug: this.params.slug, 
     produceDate: this.params.produceDate, 
     releaseDate: this.params.releaseDate 
    } 
    if (game.mode) { 
     if (this.params.produceDate && this.params.releaseDate) { 
     this.redirect('gamePageOption1', params); 
     } else { 
     this.redirect('gamePageOption2', params); 
     } 
    } else { 
     this.redirect('gamePageOption3', params); 
    } 
    }, 
    waitOn: function() { 
     return [Meteor.subscribe('games')]; 
    }, 
    data: function() { 
     return Games.findOne({slug: this.params.slug}); 
    } 
} 

如果使用紅外光譜ν< 0.9.3注意有關中間重定向的一個問題:ref1ref2。如果是這種情況,您必須在Meteor.defer裏面重定向:Meteor.defer

Meteor.defer(function() { 
    this.redirect(...) 
}) 
+0

謝謝你的幫助!如果我正確理解你的方法,我不是100%的確定。所以,你建議有不同的路線,比如'this.route('gamePageOption1')','this.route('gamePageOption2')'具有相同的URL,但具有不同的訂閱? – user3475602 2014-09-20 17:38:07

+0

對,就是這樣。一個url可以是'/ game /:slug/viewGame /:\ produceDate?/:releaseDate?/ withMode'另一個'/ game /:slug/viewGame /:\ produceDate?/:releaseDate?/ noMode',每個都有不同的訂閱。 – 2014-09-20 23:41:48

+0

我只想補充一點,我發現了一個更好的方法(用於我的目的)。我用'this.subscribe('collectionName',this.params.slug).wait();'替換了語句,而不是重定向到不同的路由。爲我工作! – user3475602 2014-09-22 16:30:56

相關問題