rest - How do I make an Ember-Data has-many collection that is defined by another API endpoint? -


i've got model, post has many comments. response get /posts/12 pretty ember-data-friendly:

{   "post": {     "id": 12,     "title": "i love ramen",     "created_at": "2011-08-19t14:22",     "updated_at": "2011-08-19t14:22",     "body": "..."   } } 

the api post's comments, however, get /posts/12/comments, returns

{   "comments": [     {       "id": 673,       "author_id": 48,       "created_at": "2011-08-21t18:03",       "body": "me too!"     }   ] } 

what can model or adapter tell post 12's comments, use /posts/12/comments? notice post has no knowledge of comment ids.

update

in response buuda's answer, here clarifications:

the post must able comments can (a) show comments on postroute , (b) have properties on post like

hascomments: function() {   return this.get('comments.length') > 0; }.property('comments') 

it's fine me if have implement comments computed property, though. in above-mentioned answer, buuda suggests

app.comments.find({ id: postid }); 

how datastore fetch /posts/:postid/comments instead of /comments?postid=:postid?

you don't need setup relationship between models, necessarily. nested resources allow fetch appropriate data. router:

app.router.map(function() {   this.resource('posts', { path: '/posts/:post_id' }, function() {     this.route('edit');     this.resource('comments', function() {       this.route('new');     });   }); }); 

the commentsroute can model resource contained in , fetch comments post id:

app.commentsroute = ember.route.extend({    model: function() {        var post = this.modelfor('posts');        var postid = post.get('id');        return app.comments.find({ id: postid });    } }); 

the posts model doesn't need know comment ids, underlying datastore has return appropriate comments based on post id queries. returned array used model comments route.

edit:

i assume using ember-data. if so, nested resource urls (posts/:postid/comments) not yet supported. show comments in post route might want fetch comments data, set on comments controller, use controller injection ('needs') in posts controller, , use experimental 'control' handlebars tag show comments view:

app.postsroute = ember.route.extend({    setupcontrollers: function() {        var post = this.modelfor('posts');        var postid = post.get('id');        var comments = app.comments.find({ id: postid });        this.controllerfor('comments').set('content', comments);    } }); 

i explain how use experimental control tag here: how render hasmany associations own controller


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -