【发布时间】:2023-03-06 02:12:01
【问题描述】:
如何让 Ember 不仅检索匹配列表,还获取每个关联玩家的对象?
在我的路由器中,我有:
App.MatchesRoute = Ember.Route.extend
model: ->
App.Match.find()
这是我的 localhost:3000/matches.json 的 JSON 数据
{"matches":[{"id":1,"player_one":{"id":1,"name":"Lex"},"player_two":{"id":20,"name":"Superman"}}]}
更新以提供更多信息 03/15/13
这是matches.emblem(标志是haml-slim-like-handlebars)
#matches
each match in controller
linkTo "match" match class="panel six columns"
p Match between {{match.player_one.name}} and {{match.player_two.name}}
我们已成功获取Match 对象,我可以在其上调用id(未显示),但我们需要获取match.player_one 和match.player_two 的名称,它们返回未定义。
匹配
App.Match = DS.Model.extend
player_one: DS.belongsTo('App.Player')
player_two: DS.belongsTo('App.Player')
播放器
App.Player = DS.Model.extend
name: DS.attr('string')
【问题讨论】:
-
对此我不确定,但我认为您的
Player模型也必须使用belongsTo引用Match。而且您必须告诉您的适配器嵌入子对象。类似于:DS.RESTAdapter.map( 'App.Match', { player_one: { embedded: 'load' } });... 类似这样的东西.. 但我不能 100% 确定如何正确地做到这一点,我只知道是这样的(我使用的是适配器的修改版本) -
感谢您考虑这个问题,您使用修改版适配器有什么原因吗?也许这也是我应该研究的。
-
我使用不同的适配器/序列化器的唯一原因是因为我使用.NET 后端,这与 Rails 略有不同。它是 .NET 世界中Ember Web API template 的一部分。你也可以看到它在使用here。对于 Rails,使用内置的 RESTAdapter
标签: ember.js ember-data