Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ember-data really support nested urls?

I have a top level session object that can have many speakers under it.

My REST ish endpoint allows me to get all the sessions like so

/sessions/

I can get all the speakers for a given session like so

/sessions/1/speakers/

With the current version of ember-data I can see it's trying to find all the speakers with this url

/speakers/

Using the data model below

CodeCamp.Session = DS.Model.extend({
  id: DS.attr('number'),
  name: DS.attr('string'),
  speakers: DS.hasMany('CodeCamp.Speaker',{nested: true})                                                          
});

CodeCamp.Speaker = DS.Model.extend({
  id: DS.attr('number'),
  name: DS.attr('string'),
  session: DS.belongsTo('CodeCamp.Session',{nested: true})
});

I added the "nested:true" part because I wanted ember-data to build the required

/sessions/%@/speakers/

But this didn't happen -

I know I can define a url manually

CodeCamp.Speaker.reopenClass({
  url: 'sessions/%@/speakers/'
});

But in doing so I will need to roll my own "buildURL" method in the base REST adapter so it looks for a format like this and adds the parent id when necessary (and I'd rather not do this if possible)

Does ember-data have support for something like this out of the box or will I be forced to write a great deal of code myself?

like image 935
Toran Billups Avatar asked Oct 06 '12 01:10

Toran Billups


1 Answers

{nested: true} is not yet a feature. The pull request is still open.

Override & use the adapter's findQuery() to make a GET request with such a URL.

Also, if your server is Rails (and maybe this works for other frameworks), it is possible to keep the request URL single-level, by passing the parent ID as a querystring param:

/speakers?session_id=1
like image 128
Mars Avatar answered Oct 17 '22 01:10

Mars