Nested dynamic segments using RestAdapter


#1

I’ve been struggling some time to get the following situation working:

GET /folders => list of folders (folder_id, folder_name, ...)
GET /folders/:folder_id/items => list of items (item_id, item_name, ...) belonging to given folder with folder_id
GET /folders/:folder_id/items/:item_id => details (details_subject...) of item with item_id belonging to given folder with folder_id

A folder can have one or more items, but an item has only one details which is a single record with a number of fields.

I have some influence over the API so can introduce side-loading links if needed.

I’d be very thankful if someone could point me out to the best way to tackle this.


#2

You have several options.

Use buildURL()

In buildURL you have access to record as the third option. With that you can do the following:

buildURL: function (type, id, record) {
  return '/folders/' + id + '/items/' + record.get('item.id');
}

Send Links Metadata

You can implement an extractMeta where you store off the href for this record. When you implement buildURL you can just pull the metadata off the type. This is handy if you’re talking to a HAL JSON backend.

Synthetic Key

Instead of the backend issuing a single ID it can issue a sythentic id which is the combination of the 2 keys e.g. "12-42".

Here some more resources

http://codeofficer.com/blog/2013/10/03/ember-data-faking-a-composite-key.html


#3

I think that for my situation using buildURL would make the most sense. How can I distinguish within the RESTAdapter when to use this, e.g. that indeed this call is for building the items url?


#4

You can can infer the request by doing:

record.get('dirtyType'); // created, updated, deleted