Ember data 1.0.0: confused with adapters and serializers of each type

I am moving from Ember data from 0.13 to 1.0.0 beta. According to the document https://github.com/emberjs/data/blob/master/TRANSITION.md , now there are adapters of each type and type serializers.

This means that I can no longer define "myRestAdapter" with some specific overrides for the primary key and authentication. I need to implement this code for each type of model, which leads to duplication xx times in the same code.

Code in Ember 0.13 data:

App.AuthenticatedRestAdapter = DS.RESTAdapter.extend({ serializer: DS.RESTSerializer.extend({ primaryKey: function() { return '_id'; } }), ajax: function (url, type, hash) { hash = hash || {}; hash.headers = hash.headers || {}; hash.headers['Authorization'] = App.Store.authToken; return this._super(url, type, hash); } }); 

Code in Ember data 1.0.0 (only for setting the primary key to _id instead of _id:

 App.AuthorSerializer = DS.RESTSerializer.extend({ normalize: function (type, property, hash) { // property will be "post" for the post and "comments" for the // comments (the name in the payload) // normalize the `_id` var json = { id: hash._id }; delete hash._id; // normalize the underscored properties for (var prop in hash) { json[prop.camelize()] = hash[prop]; } // delegate to any type-specific normalizations return this._super(type, property, json); } }); 

I correctly understood that I need to copy the same block now for each model that requires _id as a primary key? Is there no way to specify this once for the entire application?

+6
source share
4 answers

Since code seams should be type agnostics, why don't you just create your own serializer from which your models can stretch, for example:

 App.Serializer = DS.RESTSerializer.extend({ normalize: function (type, hash, property) { // property will be "post" for the post and "comments" for the // comments (the name in the payload) // normalize the `_id` var json = { id: hash._id }; delete hash._id; // normalize the underscored properties for (var prop in hash) { json[prop.camelize()] = hash[prop]; } // delegate to any type-specific normalizations return this._super(type, json, property); } }); 

And then use App.Serializer for all of your models:

 App.AuthorSerializer = App.Serializer.extend(); App.PostSerializer = App.Serializer.extend(); ... 

Hope this helps.

+5
source

You can also install App.ApplicationSerializer . This will work if you want this normalization to apply to each model.

 App.ApplicationSerializer = DS.RESTSerializer.extend({ normalize: function (type, property, hash) { var json = { id: hash._id }; // ... return this._super(type, property, json); } }); 
+3
source

I really don't know if this is recommended, but since I need a primary key for "_id" for each model, I just did this:

 DS.JSONSerializer.reopen({ primaryKey: '_id' }); 
+3
source

I found that this works with primary key identifiers _id:

 MediaUi.ApplicationSerializer = DS.RESTSerializer.extend({ normalize: function (type, property, hash) { // property will be "post" for the post and "comments" for the // comments (the name in the payload) // normalize the `_id` var json = { id: hash._id }; delete hash._id; // normalize the underscored properties for (var prop in property) { json[prop.camelize()] = property[prop]; } // delegate to any type-specific normalizations return this._super(type, json, hash); } }); 

The difference here is that I switch the hash in the for loop to property and pass the hash to super. Maybe this is a bug with Ember Data 1.0 Beta?

0
source

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


All Articles