Ember Data - 保存记录丢失有很多关系 [英] Ember Data - Saving record loses has many relationships

查看:103
本文介绍了Ember Data - 保存记录丢失有很多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ember Data Fixture Adapter进行问题。保存记录时,所有记录的许多关联都会丢失。我创建了一个简单的JS Bin来说明这个问题: http://jsbin.com/aqiHUc/42/edit



如果您编辑任何用户并保存,则所有项目都将消失。



正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆