Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4831

Serialization of deeply nested object

$
0
0

@Karol wrote:

Hi,

I have a model class Item which looks like this:

export default class ItemModel extends Model {
  @attr name;
  @attr description;
  @attr quantityInfo;
}

QuantityInfo has some nested attributes but since it is embedded object with no id I don’t want to create another model for it (according to https://guides.emberjs.com/release/models/defining-models/#toc_read-only-attributes ).

I’m connecting with json API which uses snake case so I made ItemSerializer which extends JSONSerializer.extend(EmbeddedRecordsMixin). In this serializer I add method:

export default class ItemModel extends Model {
  @attr name;
  @attr description;
  @attr quantityInfo;

  keyForAttribute(key) {    
    return underscore(key);
  }
}

.

The problem is that nested attributes are not serialized in the underscore way. This is the output json:

{
   "name":"Cola 0.5",
   "description":"asd",
   "quantity_info":{
      "defaultQuantity":0
   }
}

Is there any simple solution to solve this problem?

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4831

Trending Articles