6
votes

I'm really struggling with Ember-data's REST adapter and nested resources and would appreciate any help... I'm using the latest (1.0.4pre) version of Ember and Ember-data (revision: 11).

I have 2 simple models:

App.User = DS.Model.extend({
  name:     DS.attr('string'),
  email:    DS.attr('string'),
  schemes:  DS.hasMany('App.Scheme')
});

App.Scheme = DS.Model.extend({
  name: DS.attr('string'),
  users: DS.hasMany('App.User')
});

and simple nested routes:

Router.map(function() {
  this.resource("user", { path: "users/:user_id" }, function() {
    this.resource("schemes");
  });
});

I have a Rails 3 JSON API which has the following endpoints:

/users/:user_id
/users/:user_id/schemes

When I navigate to users/:user_id/schemes in my ember app I want to retrieve all user's schemes, however by default Ember-data requests /schemes, which isn't an endpoint in my API - I can add it but I need to scope schemes by user, I do not have the concept of current_user as I want to view any user and her schemes.

Is there a way of either forcing Ember to look at /users/:user_id/schemes or to append a query parameter to Ember's default ajax call, eg: /schemes?user_id=:user_id?

Alternatively, is there a way of adding to the ajax request headers, which in turn my rails API can read and set up the /schemes response accordingly?

1

1 Answers

4
votes

It seems like this feature has been discussed quite a bit, but not yet implemented. This issue contains sample code for a quick work-around.

You could also use:

var userSchemes = App.Scheme.find({ used_id: 'id' });

to query /schemes?user_id=id.