我无法在hasMany / belongsTo关系中保存父记录。当我保存记录时,它会忘记它的孩子是谁。
对象的定义如下:
// We're using ember-data...
MF.Store = DS.Store.extend({
adapter: DS.FixtureAdapter
});
// Two objects, a parent...
MF.Parent = DS.Model.extend({
name: DS.attr(),
childs: DS.hasMany('child')
});
MF.Parent.FIXTURES = []; // ...with fixtures
// ...and a child
MF.Child = DS.Model.extend({
name: DS.attr(),
parent: DS.belongsTo('parent')
});
MF.Child.FIXTURES = [];
我尝试了以下操作顺序,每个操作的结果相同。
答案 0 :(得分:0)
看起来这是一个已知错误:http://discuss.emberjs.com/t/ember-data-fixture-adapter-saving-record-loses-has-many-relationships/2821
以下是他们建议的修复方法。它似乎正在发挥作用。
MF.jsonSerializer = DS.JSONSerializer.extend({
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
}
}
});
然后我只为我需要的模型指定序列化器:
MF.ParentSerializer = MF.jsonSerializer;
MF.Childerializer = MF.jsonSerializer;