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

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

问题描述

我很难在 ember 数据中保存一对多的关系.我有这样的关系:

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()

然后我想看到一个带有这样的负载的 PUT 请求:

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

api/ParameterSets/5

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

但我得到了这个:

{parameterSet: {name: "foo"}}

我不关心从孩子到父母的关系,但是如果我将 parameterSet: DS.belongsTo("App.ParameterSet") 添加到 App.Region 模型中,那么我会得到 2 PUT对两个新关系的区域 url 的请求,这并不是我真正想要的.

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

推荐答案

hasMany 关系的序列化由 json_serializer 的 addHasMany() 方法处理.js.

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

以下注释包含在源代码中:

默认的 REST 语义是只添加多对多关系,如果它被嵌入.如果关系最初是由 ID 加载的,我们假设这是作为性能优化完成的,并且更改为has-many 应该保存为孩子所属的外键更改关系.

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' }
        });
      }
    })
  })
});

当然,现在您的后端需要将关联区域的 JSON 实际嵌入到参数集的 JSON 中.如果您想保持原样,您可以简单地使用自定义序列化覆盖 addHasMany().

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天全站免登陆