2015-05-14 72 views
0

我有一個使用ember-cli 0.2.3創建的Ember 1.11應用程序。我在router.js文件中的以下內容:獲取路由中父對象的ID

this.route('ownedGames', function() { 
    this.route('gamePlays', {path: ":owned_game_id/plays"}, function() { }); 
}); 

這允許我使用以下網址:

http://localhost:4200/ownedGames/1/plays 

當我訪問該網址,我收到以下錯誤:

Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed '<[email protected]:owned-game::ember470:1>' (wrapped in (generated ownedGames.gamePlays controller)) 

這裏是我的遊戲路線

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model: function(params) { 
     console.log("Getting game plays from store"); 
     return this.store.find('gamePlay', {ownedGame: params.owned_game_id}); 
    } 
}); 

這是我是如何模仿我的燈具數據:

OwnedGame.reopenClass({ 
    FIXTURES: [ 
     { id: "1", rating: "8.25", game: "1", plays: [1,2]}, 
     { id: "2", rating: "8.25", game: "2", plays: []}, 
     { id: "3", rating: "8.25", game: "3", plays: []}, 
     { id: "4", rating: "8.25", game: "4", plays: []} 
    ] 
}); 

GamePlay.reopenClass({ 
    FIXTURES: [ 
     {id: "1", date: "2015-01-01", ownedGame: "1"}, 
     {id: "2", date: "2015-02-01", ownedGame: "1"} 
    ] 
}); 

回答

0

在您設置:owned_game_dd是在ownedGames.ownedGame路線可供你的路由器的地圖,因此參數不會在ownedGames.ownedGame.gamePlays路線可作爲你會想。爲了使參數形成另一條路線,您必須從transition對象中獲取它們作爲model方法的第二個參數。

App.Router.map(function() { 
    this.resource('ownedGames', function() { 
    this.route('ownedGame', {path: '/:owned_game_id'}, function() { 
     this.route('gamePlays', {path: '/plays'}, function() { }); 
    }); 
    }); 
}); 

App.OwnedGamesOwnedGameGamePlaysRoute = Ember.Route.extend({ 
    model: function (params, transition) { 
     var id = transition.params['ownedGames.ownedGame'].owned_game_id; 

     return this.store.find('gamePlay', id); 
    } 
}); 

http://emberjs.jsbin.com/favucawadi/1/edit?html,js,output

+0

轉換未定義。所以這不起作用。 – Gregg

+0

@Gregg你應該有它可用https://github.com/emberjs/ember.js/blob/bfcc15ee3ac2b2b3e4dad6aa0d7a447936302f5a/packages/ember-routing/lib/system/route.js#L1327。如果你可以設置一個jsbin,我們可以調試furthur –

+0

謝謝。我做了足夠的改變,我不確定這個問題有多有效。我會看看我是否可以編輯它是有道理的。 – Gregg

0

如果您ownedGame路由設置它的模型,你可以抓住通過modelFor()參考模型和拔斷其ID。這會將您從父路由的實際參數名稱中抽離出來。

Ember沒有關於轉換實例上的私有和公共的文檔,但是他們通過參數傳遞的事實以及向模型鉤子的轉換表明,從轉換對象中獲取params不是100%安全的除非你在過去的幾個月內沒有貶低折扣 - 通知突破 - 變化。

另外,this.resource()在1.13中被棄用;如果這是一個新項目,我會一路選擇this.route()。

+0

你有關於資源棄用的鏈接嗎? – Gregg

+0

http://discuss.emberjs.com/t/router-this-resource-gone-from-documentation-v1-11-0/7833/2 –