2
votes

Firstly, I will be using ember-cli for development and I get JSON from the server in embedded format:

Single Objects

{
   "id" : 1,
   "name" : "Tom",
   "surname" : "Smith",
   "address" : {
       "id" : 2,
       "street" : "23",
       "city" : "...",
       ...
   }
}

Arrays

[

{
   "id" : 1,
   "name" : "Tom",
   "surname" : "Smith",
   "address" : {
       "id" : 2,
       "street" : "23",
       "city" : "...",
       ...
   }
},
{
   "id" : 2,
   "name" : "Tom",
   "surname" : "Smith",
   "address" : {
       "id" : 4,
       "street" : "23",
       "city" : "...",
       ...
   }
},
...

]

I have worked out how to append the prefix onto each payload using the following in RestAdapter.

export default DS.RESTSerializer.extend({
    extract : function(store, type, payload, id, requestType) {

        var typeKey = type.typeKey,
            typeKeyPlural = typeKey.pluralize();

        //Add name of model to payload as expected by ember data
        var modelName = Array.isArray(payload) ? typeKeyPlural : typeKey;
        var normalizedPayload = {};
        normalizedPayload[modelName] = payload;

        return this._super(store, type, normalizedPayload, id, requestType);

    },
)}

I have searched around all over the place and can see all these different ways of embedding records in ember.

The official docs say to us the DS.EmbeddedRecordsMixin Class. But this would mean I would need to create a DS.ActiveModelSerializer for every single model, I would rather define the attribute in the model itself {embedded : "always"}. http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

This actually sort of worked but is obviously old because the parameters have since changed. It is a bit of a hack in my opinion. http://mozmonkey.com/2013/12/serializing-embedded-relationships-ember-data-beta/

There is this project, but no docs or example of how to get it working. https://github.com/pixelhandler/ember-data-extensions

All in all I am very confused.

1

1 Answers

0
votes

The correct approach is to use a serializer and the DS.EmbeddedRecordsMixin.

You are really fighting against separation of concerns by specifying serializer options in your models.

So, that said, lets get down to the evil.

You can simply copy DS.EmbeddedRecordsMixininto your application and modify the way it checks its options to instead examine the relationship meta data on the model type. You can then extendyour default ApplicationSerializer with your custom mixin so that you don't have to specify a serializer for all your models.

You will need to modify the following function, from this:

  attrsOption: function(attr) {
    var attrs = this.get('attrs');
    return attrs && (attrs[camelize(attr)] || attrs[attr]);
  }

To something like this (note the extra param):

  attrsOption: function(attr, type) {
    var meta = type.metaForProperty(attr) || type.metaForProperty(camelize(attr));
    return meta && meta.options;
  }

You will also need to modify all the callers of attrsOption to pass the model type down, but you will then have your very own embedded record mixin that gets its options from the model type.

Then when specifying your model relationships you can use the embedded record options as follows:

  address: DS.belongsTo('address', { embedded: 'always' })