Question

I'm doing my first hasMany relationship with ember-data and hit the always fun

"Uncaught Error: assertion failed: Your server returned a hash with the key 0 but you have no mappings"

This usually means I don't have the json structure in what I call an "ember" friendly format.

I'm building my own REST adapter for django using the django rest framework so I'm curious what this should look like to sideload without error.

Currently the json coming back looks like the below (clearly no tie back to it's session but maybe ember already knows how to wire this up?)

[{"id": 2, "name": "FooBar"}]

The models look like this

CodeCamp.Session = DS.Model.extend({
    id: DS.attr('number'),
    name: DS.attr('string'),
    room: DS.attr('string'),
    desc: DS.attr('string')
});                 

CodeCamp.Speaker = DS.Model.extend({
    id: DS.attr('number'),
    name: DS.attr('string'),
    session: DS.belongsTo('CodeCamp.Session')
}); 

CodeCamp.Session.reopen({
    speakers: DS.hasMany('CodeCamp.Speaker')
});

Thank you in advance

Was it helpful?

Solution

The json structure should look like this

{ speakers: [{ id: 2, name: "FooBar" }] }

Found this commit that shows I just needed to wrap my json inside a named dict

https://github.com/Kurki/data/commit/f59ad5bc9718634b6f3d59356deae0bf97a1bbd5

So this is my custom json method now in my django adapter

 findMany: function(store, type, ids) {
            var root = this.rootForType(type), plural = this.pluralize(root), json = {};
            this.django_ajax(this.buildURL(root, ids), "GET", {
                success: function(pre_json) {
                    json[plural] = pre_json;                                                                       
                    this.sideload(store, type, json, plural);
                    store.loadMany(type, json[plural]);
                }
            });
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top