Ember-data Serialize / Deserialize inline records at 3rd level
Excuse me for coming up with this title, but I really don't know how to ask about this, so I'll just explain. Model: Group (is) User (is) Message Defined as:
// models/group.js
name: DS.attr('string'),
// models/user.js
name: DS.attr('string'),
group: DS.belongsTo('group')
// models/post.js
name: DS.attr('string'),
user: DS.belongsTo('user'),
When I request /posts
, my server returns this inline entry:
{
"posts": [
{
"id": 1,
"name": "Whitey",
"user": {
"id": 1,
"name": "User 1",
"group": 2
}
}
]
}
Note that it group
does not have a group entry, but instead.
With my serializers:
// serializers/user.js
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
group: {embedded: 'always'}
}
});
// serializers/post.js
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
user: {embedded: 'always'}
}
});
This suggests that in the models User
and Post
have built-in recording. However, this introduces a problem as there is no inline entry in the json response group
.
The question is, is it possible to turn off attachment records at 3rd level?
Please, help.
source to share
Here's a good article on serialization:
http://www.toptal.com/emberjs/a-thorough-guide-to-ember-data#embeddedRecordsMixin
Ember docs:
http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html
Basically what it says is that you have 2 options 1) serialize and 2) deserialize. These two have 3 options:
- 'no' - do not include any data,
- 'id' or 'ids' - enable id (s),
- 'records' - include data.
When you write {embedded: 'always'}
, it is reduced to: {serialize: 'records', deserialize: 'records'}
.
If you do not want all sent messages recorded: {serialize: false}
.
The default Ember settings for EmbeddedRecordsMixin are as follows:
BelongsTo: {serialize:'id', deserialize:'id'}
HasMany: {serialize:false, deserialize:'ids'}
source to share