如何使嵌入式hasMany关系与ember数据一起使用

Ria*_*iad 17 javascript ember.js ember-data

我无法embedded hasMany使用ember数据正常工作.

我有类似的东西

App.Post = DS.Model.extend({
  comments: DS.hasMany('App.Comment')
});

App.Comment = DS.Model.extend({
  post: DS.hasMany('App.Post'),
  name: attr('string')
});
Run Code Online (Sandbox Code Playgroud)

我的API返回以下内容GET /post:

[
  {
   id: 1
   comments: [{name: 'test'}, {name: 'test2'}]
  },
  ...
]
Run Code Online (Sandbox Code Playgroud)

我需要发送以下内容POST /post:

[
  {
    comments: [{name: 'test'}, {name: 'test2'}]
  },
  ...
]
Run Code Online (Sandbox Code Playgroud)

我想与Ember模型合作并让他们提出适当的要求:

var post = App.store.createRecord(App.Post, hash_post_without_comments);
post.get('comments').createRecord(hash_comment);

App.store.commit(); // This should call the POST api 
Run Code Online (Sandbox Code Playgroud)

var posts = App.store.find(App.Post); // This should call the GET api 
Run Code Online (Sandbox Code Playgroud)

当我尝试类似的东西时post: DS.hasMany('App.Post', {embedded: true}),GET正在工作,但POST正试图为两个记录而不仅是父记录进行POST.

编辑:我的真实用例

1-我刚刚从master构建了ember数据

2-我的适配器:RESTAdapter

3-序列化程序:JSONSerializer

4-我补充道

App.MyAdapter.map('App.Join', {
    columns: { embedded: 'always' }
});
Run Code Online (Sandbox Code Playgroud)

5-我的模型是:

App.Join = DS.Model.extend({
    rowCount: DS.attr('number'),
    columns: DS.hasMany('App.JoinColumn'),
});

App.JoinColumn = DS.Model.extend({
    join: DS.belongsTo('App.Join')
});
Run Code Online (Sandbox Code Playgroud)

6-何时:

var a = App.Join.find(1);
a.get('columns').createRecord({});
App.store.commit();
Run Code Online (Sandbox Code Playgroud)

发送了一个用于joincolumn的POST,并且父级不是脏的

我错过了什么?

Yeh*_*atz 46

在master上,正确的API是:

App.Adapter.map('App.Post', {
  comments: { embedded: 'always' }
});
Run Code Online (Sandbox Code Playgroud)

两个可能的值embedded是:

  • load:加载时嵌入子记录,但应保存为独立记录.为了使其工作,子记录必须具有ID.
  • always:加载时嵌入子记录,并将其保存在同一记录中.当然,这会影响记录的肮脏(如果子记录发生更改,适配器会将父记录标记为脏记录).

如果您没有自定义适配器,可以map直接拨打DS.RESTAdapter:

DS.RESTAdapter.map('App.Post', {
  comments: { embedded: 'always' }
});
Run Code Online (Sandbox Code Playgroud)

  • @YehudaKatz,对ember-data的新版本,通过自定义适配器映射的正确方法是什么?Adapter.map函数不再存在. (15认同)

bob*_*bey 7

我有完全一样的问题.

已在ember数据问题跟踪器上报告此错误.以下PR添加了2个显示问题的失败测试:https://github.com/emberjs/data/pull/578

似乎现在没有解决方法.

编辑:

sebastianseilund在2天前开了一个公关,解决了你的问题.看看:https://github.com/emberjs/data/pull/629/files


ste*_*ser 5

在这篇文章中添加更新,其他人遇到了这篇文章,并且很难弄清楚当前版本的ember-data有什么用处.

从Ember Data 1.0.0.beta.7开始,您需要覆盖序列化程序上的相应方法.这是一个例子:

1)重新打开序列化程序(信用到这篇文章):

DS.RESTSerializer.reopen({
  serializeHasMany: function(record, json, relationship) {
    var hasManyRecords, key;
    key = relationship.key;
    hasManyRecords = Ember.get(record, key);
    if (hasManyRecords && relationship.options.embedded === "always") {
      json[key] = [];
      hasManyRecords.forEach(function(item, index) {
        // use includeId: true if you want the id of each model on the hasMany relationship
        json[key].push(item.serialize({ includeId: true }));
      });
    } else {
      this._super(record, json, relationship);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

2)将embedded: 'always'选项添加到模型上的关系:

App.Post = DS.Model.extend({
  comments: DS.hasMany('comment', {
    embedded: 'always'
  })
});
Run Code Online (Sandbox Code Playgroud)


Pat*_*her 5

这对我有用(Ember 1.5.1 + pre.5349ffcb,Ember Data 1.0.0-beta.7.f87cba88):

App.Post = DS.Model.extend({
  comments: DS.hasMany('comment', { embedded: 'always' })
});

App.PostSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    comments: { embedded: 'always' }
  }
});
Run Code Online (Sandbox Code Playgroud)