Reputation: 5553
I have a simple (made up for this question) model with an attribute called electricGuitars that has a few defaults predefined.
export default class guitarCollectionModel extends Model {
@attr('string') firstName;
@attr('string') lastName;
@attr({
defaultValue() {
return [
{
serialNumber: "sdfdsfadf",
colorCode: "dsfsadfdfa"
},
{
serialNumber: "234234234",
colorCode: "234234234323"
},
{
serialNumber: "oorprpprprpororprp",
colorCode: "rproproroprporp"
}
];
}
}) electricGuitars;
}
When I save the model (this.model.save()
) Ember Data POSTs to my API and almost everything is serialized correctly by default (no custom serializer or adpaters). API expects all property names to be dasherized.
{
"data": {
"attributes": {
"first-name": null,
"last-name": null,
"electric-guitars": [
{
"serialNumber": "sdfdsfadf",
"colorCode": "dsfsadfdfa"
},
{
"serialNumber": "234234234",
"colorCode": "234234234323"
},
{
"serialNumber": "oorprpprprpororprp",
"colorCode": "rproproroprporp"
}
]
},
"type": "guitar-collection"
}
}
The problem is that the serialNumber
and colorCode
properties inside of the electricGuitars
property/array have not been dasherized.
Is there an easy way to achieve this?
Upvotes: 0
Views: 122
Reputation: 18240
One option would be to write a custom transform. In the serialize
function you could dasherize:
serialize(deserialized) {
return Object.fromEntries(Object.entries(deserialized)
.map(([key, val]) => [dasherize(key), val])
)
}
then specify the custom transform:
@attr('my-custom-transform', {
Upvotes: 1
Reputation: 6397
While JSON:API supports any valid JSON as an attribute value, Ember Data serialisers don’t apply to nested complex objects. You may find Ember Data Model Fragments of use for this, or embedded records.
Upvotes: 1