17

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?

Sid
  • 2,433
  • 15
  • 22
Riad
  • 235
  • 1
  • 3
  • 7

4 Answers4

46

On master, the correct API is:

App.Adapter.map('App.Post', {
  comments: { embedded: 'always' }
});

The two possible values of embedded are:

  • load: The child records are embedded when loading, but should be saved as standalone records. In order for this to work, the child records must have an ID.
  • always: The child records are embedded when loading, and are saved embedded in the same record. This, of course, affects the dirtiness of the records (if the child record changes, the adapter will mark the parent record as dirty).

If you don't have a custom adapter, you can call map directly on DS.RESTAdapter:

DS.RESTAdapter.map('App.Post', {
  comments: { embedded: 'always' }
});
MasterMastic
  • 19,099
  • 11
  • 59
  • 86
Yehuda Katz
  • 27,905
  • 12
  • 85
  • 91
  • Is this the same for belongsTo as well ? – ken Jan 15 '13 at 15:53
  • Yes. It is the same API for embedded belongsTo. – Yehuda Katz Jan 15 '13 at 16:06
  • 1
    I think there may be a bug when persisting embedded records, https://github.com/emberjs/data/pull/578 – sandstrom Jan 16 '13 at 18:13
  • How to solve this problem http://stackoverflow.com/questions/14896049/emberjs-multi-level-hierarchy-with-embedded-always – sudhanshu Feb 15 '13 at 14:17
  • 15
    @YehudaKatz, with the new revisions to ember-data, what is the proper way to map via a custom adapter? The Adapter.map function no longer exists. – Beez Sep 26 '13 at 13:48
  • 1
    I think, we have to follow this JSON conventions. The embeded objects follow the main object. http://emberjs.com/guides/models/connecting-to-an-http-server/#toc_json-conventions – Zoltan Oct 21 '13 at 00:43
7

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

bobey
  • 98
  • 5
5

Adding an update to this incase others come across this post and are having a hard time figuring out what works with the current version of ember-data.

As of Ember Data 1.0.0.beta.7, you need to override the appropriate methods on the serializer. Here's an example:

1) Reopen the serializer (credit to this post):

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

2) Add the embedded: 'always' option to the relationship on the model:

App.Post = DS.Model.extend({
  comments: DS.hasMany('comment', {
    embedded: 'always'
  })
});
Patrick Fisher
  • 7,445
  • 5
  • 30
  • 26
steakchaser
  • 5,018
  • 1
  • 24
  • 34
5

This is what worked for me (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' }
  }
});
Patrick Fisher
  • 7,445
  • 5
  • 30
  • 26