mardi 7 juillet 2015

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.




Aucun commentaire:

Enregistrer un commentaire