8

My application backend has several resources. A model is exposed for each resource.

The entry point to all other resources is through the User model. What I mean is, given a User we can find BlogPost. Given a BlogPost we can find Comments etc.

In Ember terminology, we could say:

User hasMany BlogPost
BlogPost hasMany Comment
Comment belongsTo BlogPost

By backend exposes REST APIs of the form:

GET /api/v1/users/1
GET /api/v1/users/1/blog_posts/1
GET /api/v1/users/1/blog_posts/1/comments/1

I'm trying to figure out how to use Ember Data to fetch Comment belonging to a certain BlogPost belonging to a certain User.

From what I see, if I define a typical Ember model for Comment:

App.Comment = DS.Model.extend({
  ...
  blogPost: DS.belongsTo('App.BlogPost')
});

and in the CommentRoute I have the following:

var CommentRoute = MessageRoute.extend({
    model: function(params) {
        this.store.find('comment')
    },

The request is sent to:

/api/v1/comments

I don't even know where to begin in order for Ember Data to use urls of the form:

GET /api/v1/users/1/blog_posts/1/comments/1

I've seen several similar questions asked (see links below), but haven't seen a definitive answer to any of them. Most of them are almost a year old when ember-data, perhaps, did not have such functionality (or so it is claimed in some of these threads).

I ask again to confirm whether ember-data has such functionality or not.

Similar Questions:

  1. Ember Data nested Models
  2. Canonical way to load nested resources
  3. Deep nested routes
Community
  • 1
  • 1
Code Poet
  • 11,227
  • 19
  • 64
  • 97

1 Answers1

1

The best way to handle it is with links. If you don't want to do it like that, it is far from supported, and difficult to hack in (the pipeline just doesn't easily pass the information around). Personally I'd recommend rolling your own adapter in that case (Ember without Ember Data).

App.Foo = DS.Model.extend({
  name: DS.attr('string'),
  bars : DS.hasMany('bar', {async:true})
});

App.Bar = DS.Model.extend({
  foo: DS.belongsTo('foo'),
});

json:

{
  id: 1,
  name: "bill",
  links: {
    bars: '/foo/1/bars'
  }
}

Example: http://emberjs.jsbin.com/OxIDiVU/971/edit

Community
  • 1
  • 1
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • How does the `links` attribute help? We can figure out that route straight in the template. – givanse Jul 01 '14 at 18:53
  • I'm sorry, I'm not sure if I understand what you're saying. I'm not sure if you're mixing ember terminology with REST terminology or just talking about something completely different. What does the template have to do with the model's endpoint? – Kingpin2k Jul 01 '14 at 19:27
  • Hi @Kingpin2k I am having the same problem. Can you give us an example how you using links for nested resources. The documentation does not have any details about it. Thanks in advance – hasib32 Aug 25 '14 at 13:34
  • @Kingpin2k, do you have any recommended reading for writing a custom adapter? I'm in the same boat (working with deep nested routes) and ember-data just isn't cutting it for me. – ToddSmithSalter Aug 28 '14 at 16:29
  • Toran Billups has been working on a simple store, https://github.com/toranb/simple-ember-store/blob/master/store.js#L53, If you scroll down to the bottom half of this answer http://stackoverflow.com/questions/17938294/how-do-you-create-a-custom-adapter-for-ember-js/17938593#17938593, I mention custom adapters for a bit, they are fairly simple to start up – Kingpin2k Aug 28 '14 at 16:32
  • And this story as well, http://stackoverflow.com/questions/24408892/ember-without-ember-data/24411550#24411550 – Kingpin2k Aug 28 '14 at 16:33