1

I got an issue when i try to add an embedded belongTo to an unsaved record. When committing the transaction, I got two POST request. I don't know if I'm doing something wrong or not...

Here's my models and mapping:

Comment = App.Comment = DS.Model.extend({
  title: DS.attr('string')
});

Group = App.Address = DS.Model.extend({
  name: DS.attr('string')
});

Post = App.Post = DS.Model.extend({
  title: DS.attr('string'),
  comments: DS.hasMany(Comment),
  group: DS.belongsTo(Group)
});

DS.RESTAdapte.map(Post, {
  comments: { embedded: 'always' },
  group: { embedded: 'always' }
});

My transaction

var transaction = store.transaction();

var post = transaction.createRecord(Post, {
  title: 'This post is unsaved'
});

post.get('comments').createRecord({
    title: 'This embedded record is also unsaved'
});

post.set('group', Group.createRecord({
    name: 'My Group'
}));

transaction.commit();

Then I will have two POST request, one on '/post' wich is good and another one on '/group'.

Am I doing something wrong ? Thanks you !

4

1 回答 1

0

在我看来,您有 2 笔交易:

  • 您手动创建并包含帖子和评论的那个
  • 您在其上创建组的商店的默认商店

您可能希望您的代码是这样的:

post.set('group', transaction.createRecord(App.Group, {
    name: 'My Group'
}));

Group.createRecord将使用商店的默认交易。

于 2013-05-17T18:08:51.500 回答