Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 568
jrabary Avatar asked Apr 19 '26 21:04

jrabary


1 Answers

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.

like image 200
xdissent Avatar answered Apr 24 '26 09:04

xdissent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!