Serialize ids when saving emberjs model

放肆的年华 提交于 2020-01-07 05:28:06

问题


I have an emberjs application backed by a nodejs server and mongodb. Currently my database is sending documents with an '_id' field. I have the followign code to force Ember to treat '_id' as the primary key:

App.ApplicationSerializer = DS.RESTSerializer.extend({
   primaryKey: '_id'
});

On the other hand i have two models related by a 'hasMany' relationship as such:

App.Player = DS.Model.extend({
 name: DS.attr('string'),
 thumbLink: DS.attr('string'),
 activeGame: DS.belongsTo('game', { async: true }),
 email: DS.attr('string'),
 firstName: DS.attr('string'),
 lastName: DS.attr('string'),
 admin: DS.attr('boolean')
});

App.Game = DS.Model.extend({
  name: DS.attr('string'),
  active: DS.attr('boolean'),
  players: DS.hasMany('player', { async: true })
});

The problem is that when i try to save the model ( this.get('model').save() )on my controller the ids are not serialized and the result is ember sending the following:

{"game":{"name":"Indie/Rock","active":false,"players":[],"_id":"53cbbf43daa978983ee0b101"}}

As you can see the players array is empty, and as a result, the server is saving that empty array which in fact is not correct. I am aware that it is possible to use { embedded: true } on the models and return the models with embedded documents from the server, but i want to preserve the async feature.

I have tried to extend the game serializer from EmbeddedRecordsMixing as the following:

App.GameSerializer = DS.ActiveModelSerializer
                   .extend(DS.EmbeddedRecordsMixin)
                   .extend({
                     attrs: {
                       players: {serialize: 'ids', deserialize: 'ids'},
                     }
                   });

But when i do so i get the following error from ember even though the ApplicationSerializer is suppossedly telling Ember to user _id as primary key:

Assertion Failed: Error: Assertion Failed: You must include an `id` for App.Game in a hash passed to `push` 

My question is if it is possible to maintain the async features of ember-data while being able to serialize the document with the ids on it's relation and using _id as a primary key.

Thank you.


回答1:


Ember Data is stupid in this aspect, if it's a ManyToOne relationship, it only includes the id from the belongsTo side. Honestly I've had it on my bucket list to submit a PR, but time is limited.

https://github.com/emberjs/data/commit/7f752ad15eb9b9454e3da3f4e0b8c487cdc70ff0#commitcomment-4923439

App.ApplicationSerializer = DS.RESTSerializer.extend({

 serializeHasMany: function(record, json, relationship) {
    var key = relationship.key;
    var payloadKey = this.keyForRelationship ? this.keyForRelationship(key, "hasMany") : key;
    var relationshipType = RelationshipChange.determineRelationshipType(record.constructor, relationship);

    if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany'
        || relationshipType === 'manyToOne') {  // This is the change
      json[payloadKey] = get(record, key).mapBy('id');
      // TODO support for polymorphic manyToNone and manyToMany relationships
    }
  },
});


来源:https://stackoverflow.com/questions/24851224/serialize-ids-when-saving-emberjs-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!