Saving a model breaks one to many relationships

后端 未结 1 1062
遇见更好的自我
遇见更好的自我 2021-01-16 20:09

When I save a Parent object (which has many Child objects), the relationship is seemingly broken. The Ember docs are silent on this issue.

What do I need to do so th

相关标签:
1条回答
  • 2021-01-16 20:40

    This is an issue with the JSONSerializer and hasMany relationships. You can check this 'fixed' jsbin: http://jsbin.com/zodorule/13

    Refer to this issue: http://discuss.emberjs.com/t/ember-data-fixture-adapter-saving-record-loses-has-many-relationships/2821

    I added this to your code and it works:

    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
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题