【发布时间】:2012-12-28 13:25:25
【问题描述】:
我无法让 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')
});
我的 API 为 GET /post 返回以下内容:
[
{
id: 1
comments: [{name: 'test'}, {name: 'test2'}]
},
...
]
我需要用POST /post发送这个:
[
{
comments: [{name: 'test'}, {name: 'test2'}]
},
...
]
我想使用 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
和
var posts = App.store.find(App.Post); // This should call the GET api
当我尝试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' }
});
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 已发送且父级不脏
我错过了什么?
【问题讨论】:
-
还有其他人阅读这篇文章,看看:vinay.io/static/blog/2013/12_17.html
标签: javascript ember.js ember-data