Ember data: Is there a smart way to deal with very simple embedded objects

I have an ‘account’ object, which represents the currently logged in user. So, there will never be more than one account object. That object looks something like:

  "name": "user1",
  "quota": {
    "total": 1000,
    "used": 20
  // a few more similar embedded objects....

Do I really need to create a separate model for ‘quota’ and every other embedded object, assign a ‘belongsTo’ relation and somehow assign a made up id, or is there a good way to push this to the store und use it as it is?

1 Like

There is currently no nice way to do it. I created an issue long time ago about this: https://github.com/emberjs/data/issues/1879 :smile:

The simplest way is to create a custom Raw Transform, but last time I did that, it was a bit limited because the model didn’t become dirty when I changed a property in this raw data.

You might take a look at my model fragments project. You do currently have to create a separate model ‘fragment’ for each embedded object, but I feel it’s a small price to pay for what you get.

1 Like

A simple option is to use a transform. We have an object and array

export default Transform.extend({
  deserialize: function(serialized) {
    return serialized;
  serialize: function(deserialized) {
    return deserialized;

See other transforms as a reference, in this case we simply return the serialized/deserialized object directly, no need to do any transforms. For the array, you might want to return Em.A(serialized) if you’re not extending the prototypes.

This is only useful when you only care about the child object in the context of the parent object and not as it’s own model.

I would go with a custom serializer to convert your payload to what ember-data is expecting. If that seems like a larger ordeal, then definitely try out model fragments. This retains the isDirty property and rollback properly, which you won’t get in a simple transform on an primitive array or POJO.

I forgot to mention. In your data you could simply use ‘object’ or ‘array’ for as you would for boolean, string, date and number.

  quota: DS.attr('object'),
  comments: DS.attr('array')

@MiguelMadero unless this is a new additional to ember-data, I believe this is the same as typing DS.attr()