emberjs/data — pull request #1078 by sidonath (2013-07-09 15:39:38+00:00)
Title: Support for customizable model URLs in RESTAdapter

Here's another stab at the problem of customizable URLs in RESTAdapter (previous attempts: #186, #257, #775, #790).

This pull request is meant to start a discussion about implementing the customizable (nested) routes per model. The code quality is sub-standard and if we can find a satisfying solution I'd be happy to improve it. The important part is that the URLs are configured on the adapter level and not on models as suggested by @tomdale.

The idea is that a developer would be able to configure the URLs per resource. The URL can contain a dynamic segment (`/groups/:group_id/people`) which would be substituted with the attribute from a record being persisted or from an object that's passed as a query (e.g. `People.find({ group_id: 1 })`).

Additionally, developers would be able to extend the provided `URLBuilder` class and override how URLs for different actions are generated. This would allow, among other things, to write a client for an API that uses shallow nested resources typical for Rails apps.

There are at least two models I'd like ember-data to support:
* nested URLs typical as generated by Rails (e.g. `GET posts/:post_id/comments/:comment_id`)
* shallow nested URLs as generated by Rails (`GET posts/:post_id/comments` **but** `GET comments/:comment_id`)

To support shallow routes with this pull request, I created the following class:

```javascript
RailsShallowURLBuilder = Ember.Object.extend({
buildUpdateURL: function(record, suffix, data) {
return this.buildURL(record, suffix);
},

buildDeleteURL: function(record, suffix, data) {
return this.buildURL(record, suffix);
},

buildFindURL: function(record, suffix, data) {
return this.buildURL(record, suffix);
},

buildFindAllURL: function(record, suffix, data) {
return this.buildURL(record, suffix);
}
});
```

As you can see, in all cases where we don't want nested URL I don't pass `data` argument to `buildURL` which is an ugly hack. I'd like to see a better solution. Suggestions, please?

cc @igorT as you mentioned you would be making URLs overridable so maybe you have some suggestions.