Ember Data - 保存记录丢失有很多关系 [英] Ember Data - Saving record loses has many relationships
问题描述
如果您编辑任何用户并保存,则所有项目都将消失。
正在使用Ember 1.0.0和最新的Canary数据库。
我不知道我做错了什么,或者这是Ember Data的问题。
谢谢
为了回答我的问题, DS.JSONSerializer.serializeHasMany
似乎只处理和序列化manyToNone和manyToMany关系类型。您可以使用模型的自定义序列化程序来覆盖此行为:
var get = Ember.get;
App.UserSerializer = DS.RESTSerializer.extend({
serializeHasMany:function(record,json,relationship){
var key = relationships.key;
var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor,relationship);
if(relationshipType ==='manyToNone'|| relationshipType ==='manyToMany'|| relationshipType ==='manyToOne' ){
json [key] = get(record,key).mapBy('id');
}
}
});
仍然不太确定这是否是一个错误,或者这是所需/预期的行为。
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.
This is using Ember 1.0.0 and the latest canary build of Ember Data.
I am not sure if I am doing something wrong or if this is an issue with Ember Data.
Thanks
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 - 保存记录丢失有很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!