3
votes

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

1
I am experiencing thesame issue when using rails nested route with ember-data. Did you solve this and can you post the solution and mark it as the accepted answer for the benefit of others. thanksbrg
AFAIK nested resources are still not supported by ember-data. I guess one could instantiate individual adapters (maybe even stores have to be "duplicated") for each nested model and dynamically assign them baseURLs. I haven't tried this since it also looks like a bit of a long-shot.Martin Westin

1 Answers

1
votes

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.