Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  166] [ 6]  / answers: 1 / hits: 15324  / 12 Years ago, mon, january 14, 2013, 12:00:00

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?


More From » ember.js

 Answers
8

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


[#80889] Saturday, January 12, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gavenmekhio

Total Points: 732
Total Questions: 89
Total Answers: 93

Location: Central African Republic
Member since Mon, Aug 10, 2020
4 Years ago
;