Ste.
Ste.

Reputation: 21

ember.js JSONAPIAdapter with hasMany

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.

The 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"
                        }
                    }
                ]
            }
        }
    ]
}

The models:

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')
    });

When I used the ember-inspector to see the data only the model "altersgruppe" has records. The model "tarifbeitrag" has no records.

So why?

The adapter:

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

It seems to me that the "hasMany"-Relationship does not work. How to fix that. Any ideas (JSON wrong? Model wrong). Thank you.

Upvotes: 0

Views: 551

Answers (2)

Ste.
Ste.

Reputation: 21

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"
            }
        }
    ]
}

This works with the DS.JSONAPIAdapter. Now in the ember-inspector I can see that the model "tarifbeitrag" has also records.

Upvotes: 2

Mike Wilson
Mike Wilson

Reputation: 692

At first glance this doesn't look like a valid json-api response. tarifbeitraege should be a linked relationship, not embedded in the actual attributes of the data type. I don't believe json-api supports embedded attributes like that. Take a look at compound documents part of the spec for an example of what your json output should be.

Upvotes: 0

Related Questions