问题描述
我在使用 Ember 数据夹具适配器时遇到问题.保存记录时,所有记录的 hasMany 关联都丢失了.我创建了一个简单的 JS Bin 来说明这个问题:http://jsbin.com/aqiHUc/42/edit
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 illustrate the issue: http://jsbin.com/aqiHUc/42/edit
如果您编辑任何用户并保存,所有项目都会消失.
If you edit any of the users and save, all the projects disappear.
这是使用 Ember 1.0.0 和 Ember Data 的最新金丝雀版本.
This is using Ember 1.0.0 and the latest canary build of Ember Data.
我不确定我是否做错了什么,或者这是否是 Ember Data 的问题.
I am not sure if I am doing something wrong or if this is an issue with Ember Data.
谢谢
推荐答案
为了回答我的问题,DS.JSONSerializer.serializeHasMany
似乎只处理和序列化 manyToNone 和 manyToMany 关系类型.您可以通过为模型使用自定义序列化程序来覆盖此行为:
To answer my question, the DS.JSONSerializer.serializeHasMany
seems to only processes and serialize manyToNone and manyToMany relationship types. You can override this behaviour by using a custom serializer for the model:
var get = Ember.get;
App.UserSerializer = DS.RESTSerializer.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] = get(record, key).mapBy('id');
}
}
});
仍然不太确定这是否是一个错误,或者这是否是期望/预期的行为.
Still not quite sure if this is a bug or if this is the desired/expected behaviour.
这篇关于Ember Data - 保存记录丢失有很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!