Custom URIs using ids


#1

Hello, I have full control over a rails api and an emberjs front end.

Currently lets say we have a resource named foo that can be found only by id in the api and has a name attribute. Is it possible to implement an ember route

/foos/:foo_identifier

so that the request in store is done using the id param I pass to the route but the uri in the browser takes the name of another attribute. In short the following scenario:

Starting template {{link-to "foos.show" foo}} 

foos.show route makes a request at the api (or the ember store):

return this.store.find('foo', params.foo.id)

the URI in the browser is constructed like

/foos/model.name or /foos/params.foo.name

If this is not possible, then is the only way to find the foo method by name:

return this.store.find('foo', foo.name)

?

Finally if we look at facebook’s front end and api, in the front end we have a URI for a user of the form:

/kon.asdf

however it is not possible to query the api by a user’s name. Does this mean that they dont allow external developers to query their api by a user’s name but they do query it by name in their app ?

Thanks


#2

On the landing route you can customize how the URL is populated using the serialize() method: http://emberjs.com/api/classes/Ember.Route.html#method_serialize That method returns an object that has a matching property to the dynamic segment and ember will populate it in the URL.

That should allow you to pass the full model and get a different URL than what you might use to do the lookup in the store.

However, if they access the route directly with the URL, your model method on the route will need to take that “name” and do a proper query to get the model to resolve. So if your page with the link-to normally resolves foo for you, you’ll need to teach your “foos.show” route how to do that as well since a user coming in with the direct URL will not be passing an already populated foo, they’ll hit your model route passing in only the name. To do this in the model you’d need to use store.query not store.find in the newest ember-data: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_query-and-queryrecord

In older ember data it would still be store.find(…{name: foo.name}), passing foo.name not wrapped in an object will try to use foo.name as the id.