Ember Data 1.0.0: what is the expected format for the toTot relationship

I have the following models:

App.Publication = DS.Model.extend({ title: DS.attr('string'), bodytext: DS.attr('string'), author: DS.belongsTo('author') }); App.Author = DS.Model.extend({ name: DS.attr('string') }); 

And the following json data:

 { "publications": [ { id: '1', title: 'first title', bodytext: 'first body', author_id: 100 }, { id: '2', title: 'second title', bodytext: 'second post', author_id: 200 } ]; } 

In Ember Data RC12, this worked (you can specify author_id OR author in json and the publication will always contain the correct author).

In Ember Data 1.0.0, this no longer works; author is always null.

In some documents, I found that - since I use "author_id" in json data (and not just for the author) - I need to specify a key in the model; Thus:

  author: DS.belongsTo('author', { key: 'author_id' }) 

This, however, does not work; the author in the publication remains invalid.

The only solution that I see at the moment is to implement a custom serializer and override author_id for the author (via normailzeId); I cannot change my internal data structure ... this way:

 App.MySerializer = DS.RESTSerializer.extend({ //Custom serializer used for all models normalizeId: function (hash) { hash.author = hash.author_id; delete hash.author_id; return hash; } }); 

Is this correct above?

+4
source share
2 answers

Ember Data 1.0 no longer performs default payload normalization. The key configuration for DS.belongsTo also been removed, so you have to implement your own serializer.

normalizeId is an internal serializer function used to convert primary keys, always available in id . You should not override this.

Instead, you can override the keyForRelationship method, which is provided for this purpose.

You can use something like the following:

 App.ApplicationSerializer = DS.RESTSerializer.extend({ keyForRelationship: function(rel, kind) { if (kind === 'belongsTo') { var underscored = rel.underscore(); return underscored + "_id"; } else { var singular = rel.singularize(); var underscored = singular.underscore(); return underscored + "_ids"; } } }); 

Note. I also renamed the serializer to App.ApplicationSerializer so that it is used as the default serializer for your application.

Finally, if you have not found it yet, check out the transition notes here: https://github.com/emberjs/data/blob/master/TRANSITION.md

If you read the transition document shortly after the initial version 1.0.0.beta.1, I would recommend looking at it again, as several additions have been added, especially regarding serialization.

+7
source

From the Ember 1.0.0 Transition Guide :

Underlined keys, _id and _ids

At 0.13, the REST adapter automatically displays the incoming keys for you. He also expected that relationships should be listed in the name_id and hasMany , which would be listed in name_ids .

If your application returns json with underlined attributes and _id or _ids for the relationship, you can extend ActiveModelSerializer and everything will work out of the box.

 App.ApplicationSerializer = DS.ActiveModelSerializer.extend({}); 

Note : DS.ActiveModelSerializer should not be confused with the ActiveModelSerializer stone, which is part of the Rails API project. A typical Rails API project with a dedicated output product and DS.ActiveModelSerializer will perform the expected normalization behavior, such as verboshing property keys in your JSON.

+1
source

Source: https://habr.com/ru/post/1501196/


All Articles