获取路由中父对象的ID

我有一个使用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"}

]

});

回答:

在您设置: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

回答:

如果您ownedGame路由设置它的模型,你可以抓住通过modelFor()参考模型和拔断其ID。这会将您从父路由的实际参数名称中抽离出来。

Ember没有关于转换实例上的私有和公共的文档,但是他们通过参数传递的事实以及向模型钩子的转换表明,从转换对象中获取params不是100%安全的除非你在过去的几个月内没有贬低折扣 - 通知突破 - 变化。

另外,this.resource()在1.13中被弃用;如果这是一个新项目,我会一路选择this.route()。

以上是 获取路由中父对象的ID 的全部内容, 来源链接: utcz.com/qa/262098.html

回到顶部