ember.js JSONAPIAdapter with hasMany [英] ember.js JSONAPIAdapter with hasMany
问题描述
我尝试使用一个{json:api} -JSON与ember.js(1.13.3)和ember-data(1.13.4)通过使用DS.JSONAPIAdapter。
I try to use a {json:api}-JSON with ember.js (1.13.3) and ember-data (1.13.4) by using the DS.JSONAPIAdapter.
JSON:
{
"data": [
{
"type": "altersgruppe",
"id": "1",
"attributes": {
"name": "ALTER_21_24",
"tarifbeitraege": [
{
"type": "tarifbeitrag",
"id": "1",
"attributes": {
"name": "REISE",
"beitrag": "12.70",
"proergaenzung": "7,00",
"gesamtbeitrag": "25.99"
}
},
{
"type": "tarifbeitrag",
"id": "2",
"attributes": {
"name": "KRANKEN",
"beitrag": "25,70",
"proergaenzung": "7,00",
"gesamtbeitrag": "25.99"
}
}
]
}
},
{
"type": "altersgruppe",
"id": "2",
"attributes": {
"name": "ALTER_25_30",
"tarifbeitraege": [
{
"type": "tarifbeitrag",
"id": "3",
"attributes": {
"name": "REISE",
"beitrag": "29,70",
"proergaenzung": "7,00",
"gesamtbeitrag": "25.99"
}
},
{
"type": "tarifbeitrag",
"id": "4",
"attributes": {
"name": "KRANKEN",
"beitrag": "28,70",
"proergaenzung": "7,00",
"gesamtbeitrag": "30.99"
}
}
]
}
}
]
}
模型:
App.Altersgruppe = DS.Model.extend({
name: DS.attr('string'),
tarifbeitraege: DS.hasMany('tarifbeitrag', {async: true})
});
App.Tarifbeitrag = DS.Model.extend({
altersgruppe: DS.belongsTo('altersgruppe'),
name: DS.attr('string'),
beitrag: DS.attr('string'),
proergaenzung: DS.attr('string'),
gesamtbeitrag: DS.attr('string')
});
当我使用ember-inspector查看数据时,只有模型altersgruppe有记录。
When I used the ember-inspector to see the data only the model "altersgruppe" has records. The model "tarifbeitrag" has no records.
为什么?
适配器:
App.AltersgruppeAdapter = DS.JSONAPIAdapter.extend({
namespace: REST_ADAPTER_NAMESPACE,
shouldBackgroundReloadRecord: function(store, snapshot){
return false;
}
});
在我看来,hasMany - 关系不起作用。如何解决这个问题任何想法(JSON错误?模型错误)。谢谢。
It seems to me that the "hasMany"-Relationship does not work. How to fix that. Any ideas (JSON wrong? Model wrong). Thank you.
推荐答案
由于Mike1o1的提示再次阅读了{json:api} -Spec,所以我做了
决定将JSON的结构更改为:
Because of Mike1o1's tip to read the {json:api}-Spec again and I made the decision to changed the structure of the JSON to this:
{
"data": [
{
"type": "altersgruppe",
"id": "1",
"attributes": {
"name": "ALTER_21_24"
},
"relationships": {
"tarifbeitraege": {
"data": [
{
"type": "tarifbeitrag",
"id": "1"
},
{
"type": "tarifbeitrag",
"id": "2"
}
]
}
}
},
{
"type": "altersgruppe",
"id": "2",
"attributes": {
"name": "ALTER_25_30"
},
"relationships": {
"tarifbeitraege": {
"data": [
{
"type": "tarifbeitrag",
"id": "3"
},
{
"type": "tarifbeitrag",
"id": "4"
}
]
}
}
}
],
"included": [
{
"type": "tarifbeitrag",
"id": "1",
"attributes": {
"name": "REISE",
"beitrag": "25,70",
"proergaenzung": "7,00",
"gesamtbeitrag": "25.99"
}
},
{
"type": "tarifbeitrag",
"id": "2",
"attributes": {
"name": "KRANKEN",
"beitrag": "25,70",
"proergaenzung": "7,00",
"gesamtbeitrag": "25.99"
}
},
{
"type": "tarifbeitrag",
"id": "3",
"attributes": {
"name": "REISE",
"beitrag": "29,70",
"proergaenzung": "7,00",
"gesamtbeitrag": "25.99"
}
},
{
"type": "tarifbeitrag",
"id": "4",
"attributes": {
"name": "KRANKEN",
"beitrag": "28,70",
"proergaenzung": "7,00",
"gesamtbeitrag": "30.99"
}
}
]
}
这适用于DS.JSONAPIAdapter。现在在ember-inspector中我可以看到模型tarifbeitrag也有记录。
This works with the DS.JSONAPIAdapter. Now in the ember-inspector I can see that the model "tarifbeitrag" has also records.
这篇关于ember.js JSONAPIAdapter with hasMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!