Ember数据保存关系 [英] Ember data saving a relationship

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

问题描述

我在保存数据中难以保存一对多的关系。我有一个这样的关系:

  App.ParameterSet = DS.Model 
name:DS.attr(string )
地区:DS.hasMany(App.Region)

App.Region = DS.Model
名称:DS.attr(string)

如果我要这样做:

  parameterSet = App.ParameterSet.find(5)
@transaction = @get(store)。transaction()
@ transaction.add(parameterSet)
region1 = App.Region.find(10)
region2 = App.Region.find(11)
parameterSet.set(name,foo)
parameterSet.get region)。pushObject(region)
@ transaction.commit()

然后我想要看到一个这样的有效载荷的PUT请求:

  api / ParameterSet / 5 

{参数集:{name:foo,regionIds:[10,11]}}

得到这个:

  {parameterSet:{name:foo}} 
/ pre>

我没有不关心从小孩到父母的关系,但是如果我在App.Region模型中添加 parameterSet:DS.belongsTo(App.ParameterSet),那么我得到2 PUT要求区域url为两个新的关系,这不是我真正的想要的。



我想这是一个多对多的关系,我不确定是否支持,但是有关如何实现我所描述的任何想法?谢谢

解决方案

hasMany 的序列化由 addHasMany()方法 json_serializer.js



以下注释包含在源代码


默认的REST语义是只添加一个has-如果
被嵌入,很多关系。如果关系最初由ID加载,我们假设
作为性能优化完成,并且对
has-many的更改应该保存为子对象的外部关键字更改为
的关系。


为了达到你想要的目的,一个选项是指出这个关系应该嵌入你的适配器。 / p>

  App.Store = DS.Store.extend({
adapter:DS.RESTAdapter.extend({
serializer:DS.RESTSerializer.extend({
init:function(){
this._super();
this.map('App.ParameterSet',{
regions :{embed:'always'}
});
}
})
})
});

当然,现在您的后端需要将相关区域的JSON实际嵌入到参数集的JSON。如果你想保持原样,你可以用自定义序列化方式覆盖 addHasMany()

  App.Store = DS.Store.extend({
adapter:DS.RESTAdapter.extend({
serializer:DS.RESTSerializer.extend({
addHasMany :function(hash,record,key,relationship){
// custom ...
}
})
})
});


I'm having difficult saving a one-to-many relationship in ember data. I have a relationship like this:

App.ParameterSet = DS.Model
    name: DS.attr("string")
    regions: DS.hasMany("App.Region")

App.Region = DS.Model
    name: DS.attr("string")

If I were to do something like this:

parameterSet = App.ParameterSet.find(5)
@transaction = @get("store").transaction()
@transaction.add(parameterSet)
region1 = App.Region.find(10)
region2 = App.Region.find(11)
parameterSet.set("name", "foo")
parameterSet.get("regions").pushObject(region)
@transaction.commit()

Then I would like to see a PUT request with payload like this:

api/ParameterSets/5

{parameterSet: {name: "foo", regionIds:[10, 11]}}

but instead I get this:

{parameterSet: {name: "foo"}}

I don't care about the relationship back from child to parent but if I add parameterSet: DS.belongsTo("App.ParameterSet") to the App.Region model then I get 2 PUT requests to the regions url for the two new relationships which is not really what I want.

I guess this is a many-to-many relationship really which I'm not sure is yet supported but any ideas on how to achieve what I've described? Thanks

解决方案

The serialization of hasMany relationships is handled by the addHasMany() method of json_serializer.js.

The following note is included in the source code:

The default REST semantics are to only add a has-many relationship if it is embedded. If the relationship was initially loaded by ID, we assume that that was done as a performance optimization, and that changes to the has-many should be saved as foreign key changes on the child's belongs-to relationship.

To achieve what you want, one option is to indicate that the relationship should be embedded in your adapter.

App.Store = DS.Store.extend({
  adapter: DS.RESTAdapter.extend({
    serializer: DS.RESTSerializer.extend({
      init: function() {
        this._super();
        this.map('App.ParameterSet', {
          regions: { embedded: 'always' }
        });
      }
    })
  })
});

Of course now your back-end will need to actually embed the associated regions' JSON within the parameter set's JSON. If you want to keep things as they are, you can simply override addHasMany() with custom serialization.

App.Store = DS.Store.extend({
  adapter: DS.RESTAdapter.extend({
    serializer: DS.RESTSerializer.extend({
      addHasMany: function(hash, record, key, relationship) {
        // custom ...
      }
    })
  })
});

这篇关于Ember数据保存关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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