如何使嵌入式多个关系与余烬数据配合使用 [英] How to make embedded hasMany relationships work with ember data

查看:74
本文介绍了如何使嵌入式多个关系与余烬数据配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得嵌入式 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')
});

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

  [
{
id:1
评论:[{name: 'test'},{name:'test2'}]
},
...
]

我需要使用 POST / post 发送:

  [
{
评论:[{name:'test'},{name:'test2'}]
},
...
]

我想使用Ember模型并让他们做出适当的请求: p>

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

App.store.commit(); //这应该叫POST api

  var posts = App.store.find(App.Post); //这应该调用GET api 

当我尝试像的帖子: DS.hasMany('App.Post',{embedded:true}),GET正在工作,但POST正在尝试为两个记录创建POST,不仅是父记录。



编辑:我的真实用例



1-我刚刚从主机建立了ember数据



2-我的适配器:RESTAdapter



3-序列化程序:JSONSerializer



4-我添加了

  App.MyAdapter.map('App.Join',{
columns:{embed :'always'}
});

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

6-何时:

  var a = App.Join.find(1); 
a.get('columns')。createRecord({});
App.store.commit();

发送连接列的POST,父级不脏



我错过了什么?

解决方案

我有完全相同的问题。 b

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



现在似乎没有解决方法。



编辑



sebastianseilund 在2天前开了一个公关,它修复了你的问题。
看看: https://github.com/emberjs/data/拉/ 629 /文件


I can't get embedded hasMany to work correctly with ember data.

I have something like this

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

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

And my API returns the following for GET /post:

[
  {
   id: 1
   comments: [{name: 'test'}, {name: 'test2'}]
  },
  ...
]

I need to send this with POST /post:

[
  {
    comments: [{name: 'test'}, {name: 'test2'}]
  },
  ...
]

I want to work with Ember models and have them make the appropriate requests:

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 

and

var posts = App.store.find(App.Post); // This should call the GET api 

When I try something like post: DS.hasMany('App.Post', {embedded: true}), the GET is working but the POST is trying to make a POST for the two records not only the parent one.

EDIT : My Real use case

1- I've just built ember data from master

2- My adapter: RESTAdapter

3- The serializer: JSONSerializer

4- I added

App.MyAdapter.map('App.Join', {
    columns: { embedded: 'always' }
});

5- My Models are:

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

App.JoinColumn = DS.Model.extend({
    join: DS.belongsTo('App.Join')
});

6- When:

var a = App.Join.find(1);
a.get('columns').createRecord({});
App.store.commit();

a POST for joincolumn is sent and the parent is not dirty

What am i missing?

解决方案

I have the exact same problem.

This bug has been reported on the ember data issue tracker. The following PR adds 2 failing tests showing the problem: https://github.com/emberjs/data/pull/578

It seems that there is no workaround right now.

EDIT:

sebastianseilund opened a PR 2 days ago which fixes your problem. Have a look at: https://github.com/emberjs/data/pull/629/files

这篇关于如何使嵌入式多个关系与余烬数据配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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