Ember Data - Saving record loses has many relationships

后端 未结 4 1014
栀梦
栀梦 2020-12-28 18:38

I am having an issue working with Ember Data Fixture Adapter. When saving a record, all of the record\'s hasMany associations are lost. I have created a simple JS Bin to ill

4条回答
  •  没有蜡笔的小新
    2020-12-28 19:11

    I hit the same problem and came to the same solution. Surely this is a bug in ember-data?

    Here's my solution, the only difference is that I'm modifying JSONSerializer, rather than extended RESTSerializer because I'm using the local storage adapter.:

    DS.JSONSerializer.reopen({
        serializeHasMany : function(record, json, relationship) {
            var key = relationship.key;
    
            var relationshipType = DS.RelationshipChange.determineRelationshipType(
                    record.constructor, relationship);
    
            if (relationshipType === 'manyToNone'
                    || relationshipType === 'manyToMany'
                    || relationshipType === 'manyToOne') {
                json[key] = Ember.get(record, key).mapBy('id');
                // TODO support for polymorphic manyToNone and manyToMany
                // relationships
            }
        }
    });
    

提交回复
热议问题