1
votes

While using the RESTAdapter, I have an Organization model which is to be embedded in the response. It appears that the default implementation of the Ember.RESTAddapter sends the id, using the same model name, but not as an object (this currently 'works'):

POST/PUT api/v1/item/{id}

{
    "item" {
        id: "1029383829"
        ...
        organization: "26044097612186763401268824297"
    }
}

I have consulted the documentation, and found that the mixin DS.EmbeddedRecordsMixin should do this, coupled with declaring embedded: "always" on the attrs or the Serializer:

models/item.js

var Item = DS.Model.extend({
    ...,
    organization: DS.belongsTo("organization", {embedded: "always"})
});

serializers/item.js:

var ItemSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
        attrs: {
            organisation: {serialize: "id", embedded: "always"}
        }
    }
);

However, when deserializing records, Ember Data complains, saying that it expected an object, but just got a string:

Assertion Failed: Expected an object as data in a call to push for app@model:organization: , but was 26044097612186763401268824297

Ultimately, I would prefer a system, likened to sideloading, wherein an additional attribute, post-fixed "_id", describes the corresponding id of an embedded record:

{
    "item": {
        id: 1,
        name: "name",
        organization_id: "26044097612186763401268824297"
        ...
    }
}

How can I allow serializing and deserializing embedded id sideloading for my Organization model?

1
You don't need to specify setialize: 'id' on attrs.organisation as it is serializer's default value.Daniel Kmak

1 Answers

1
votes

You aren't actually embedding the record, you're just specifying the id, in that case you should mark it as async.

var Item = DS.Model.extend({
    ...,
    organization: DS.belongsTo("organization", {async: true})
});

And remove your embedded records implementation.