我无法输出一对多的Ember数据。根据开发人员工具选项卡,数据将正确加载到Ember Data中。但由于某种原因,我无法访问这些类别。
游戏模型
//app/models/game.js
import DS from 'ember-data';
export default DS.Model.extend({
gameCategory: DS.belongsTo('game-category'),
gameName: DS.attr('string'),
});
gameCategory模型
//app/models/game-category.js
import DS from 'ember-data';
export default DS.Model.extend({
gameCategory: DS.attr('string'),
games: DS.hasMany('games')
});
JSON返回到model.gameCategory
{"game":
[
{"id":1,"gameName":"Monopoly","gameCategory":"Board Games"},
{"id":2,"gameName":"Chess","gameCategory":"Board Games"},
{"id":3,"gameName":"Zelda","gameCategory":"Video Games"},
{"id":4,"gameName":"Mario","gameCategory":"Video Games"}
],
"gameCategory":
[
{"id":"Board Games", "gameCategory": "Board Games", "games": [1, 2]},
{"id":"Video Games", "gameCategory": "Video Games", "games": [3, 4]}
]
}
模板
//app/templates/games.hbs
{{#each model.gameCategory as |cat|}}
<div class="cat">{{cat.gameCategory}}
{{#each cat.games as |game|}}
<div class="game">{{game.gameName}}</div>
{{/each}}
</div>
{{/each}}