jrabary
jrabary

Reputation: 2441

Ember-data, hasMany relationship, loading sub-collection, nested route

I need to load a model from a rails backend with ember-data and its default REST adaptor. I have a model that has many relationship with another one :

App.Post = DS.Model.extend({
    title: DS.attr("String");
    comments: DS.hasMany('App.Comment', {keys: 'post_ids', embbeded: true});
})

App.Post = DS.Model.extend({
    body: DS.attr("String");
    post: DS.belongsTo('App.Post');
})

The JSON model returned by the server looks like

{
  title: "a title",
  comment_ids: [1,2,3,4]
}

I need at first to load all the posts without necessarly loading the associated comments, for efficiency reason. I do this with

App.store.findAll('App.Post');

And, when I select a specific post I need to load all the comments. In the ember-data documentation, it's said that I just need to call

a_specific_post.get('comments')

When, I do this I get a very long url with all comment ids :

GET : /comments?ids%all_ids_appended_here

Of course it doesn't work and if I have a thousand of comment the url is very very long.

Is it possible to get a request that matches nested routing model of rails ? :

 GET /posts/post_id/comments

The plugin route-manager https://github.com/ghempton/ember-routemanager seems to this kind of routing. Can I use it with ember-data and how ?

Thanks

Upvotes: 3

Views: 1929

Answers (1)

xdissent
xdissent

Reputation: 949

I'm not sure embedded is what you want if you're only specifying comment IDs. It is expecting full comment objects to be passed in the JSON representation of the post model.

Upvotes: 1

Related Questions