1
votes

I have an Ember-data model called goal where the list view maps back to the RESTful API at:

GET /api/users/[id]/goal

and a singular goal record would map to:

GET /api/users/[id]/goal/[goalId]

What I'm wondering is ... what's the most graceful way of configuring this using ember-data's RESTAdapter? What I've done so far is set the application adapter (aka, adapters/application.js) to a namespace of 'api' so that offset is taken care of.

What I'm not sure of is how to ensure that the /users/[id] part of the URL is included. Obviously this is a dynamic segment which makes it more complicated although I suspect its actually not hard at all but I'm just not sure how to get started.

1

1 Answers

0
votes

Its not 100% clear what the user-id is, since user always sounds like authentication. So there are two scenarios:

The User is not about security, but can be selected on then Website before accessing the goals.

In that case you have a user model with a goals: hasMany('goal') and you should access the goals over the relation from the User. You have probably a nested Route and can access to the surrounding UserRoute's model with this.modelFor('user') from the route, and then access the goals.

On the adapter the interesting function is then findHasMany. Another simple way is just add a links object on the User with `goals: '/api/users/thisUsersId/goals/'.

For the single goal the goal should be accessible with /api/goals/id since the id should be absolutely unique.

You use the User for authentication and authorization.

In this case I would recommend you to use ember-simple-auth and implement a custom authorizer. Here you can implement the authorize function and modify the jQuery-XmlHttpRequest to contains the user part.