带有hasMany的ember.js JSONAPIAdapter

时间:2015-07-07 07:32:48

标签: ember.js

我尝试使用DS.JSONAPIAdapter将{json:api} -JSON与ember.js(1.13.3)和ember-data(1.13.4)一起使用。

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"有记录。模特" tarifbeitrag"没有记录。

为什么?

适配器:

App.AltersgruppeAdapter = DS.JSONAPIAdapter.extend({
    namespace: REST_ADAPTER_NAMESPACE,
    shouldBackgroundReloadRecord: function(store, snapshot){
        return false;
    }
});

在我看来," hasMany" -Relationship不起作用。如何解决这个问题。任何想法(JSON错误?模型错误)。谢谢。

2 个答案:

答案 0 :(得分:2)

由于Mike1o1的提示再次阅读{json:api} -Spec,我做了 决定将JSON的结构更改为:

{
    "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”也有记录。

答案 1 :(得分:0)

乍一看,这看起来不像是有效的json-api响应。 tarifbeitraege应该是一个链接关系,而不是嵌入在数据类型的实际属性中。我不相信json-api支持这样的嵌入式属性。请查看规范的compound documents部分,以获取您的json输出应该是什么的示例。