Closed borwahs closed 8 years ago
@borwahs, good question. There is not an obvious answer because it kind of depends on the situation.
In your case, since the parent id in question is a user, it might make sense to have this user accessible from a session
service. That might look like this:
// adapter
export default ApplicationAdapter.extend(UrlTemplates, {
session: Ember.inject.service(),
findAllUrlTemplate: '/users/{userId}/photos',
urlSegments: {
userId() { return this.get('session.user.id'); },
},
});
However, I don't know your application and maybe in this context the user is not session-based. For example, if you want to list a different users photos, it doesn't make sense to set the id in a global place, you'd want to pass it in. One way to do this is to use query
instead of findAll
.
// adapter
export default AppAdapter.extend(UrlTemplates, {
queryRecordUrlTemplate: '/users/{userId}/photos',
});
// somewhere probably in your route
this.store.query('photo', { userId: user.id });
Also take a look at this stackoverflow answer for a little more detail: http://stackoverflow.com/questions/34752815/emberjs-custom-adapter-using-ember-data-url-templates/34753856#34753856
I hope this helps. Please close this issue if this makes enough sense to you.
Also, if there's any way you think this could be clearer in the wiki, I'd love to hear your thoughts.
@amiel, this is very helpful! Both options are useful for me as the first one works with my session store but I also have a couple nested routes that aren't tied to the any session data.
A couple follow ups out of curiosity.
I implemented your solution with query for a nested route that doesn't use any session data.
// route
export default Ember.Route.extend({
model(params) {
return this.store.query('location', { orgId: params.org_id});
}
});
// adapter
export default ApplicationAdapter.extend(UrlTemplates, {
queryUrlTemplate: '{+host}/{+namespace}/orgs/{orgId}/locations'
});
When I used this solution, it worked great but in addition, added the orgId
as a Query String parameter to the end. I tried to debug where it was still getting added but haven't been able to find the root cause.
You can consider this issue closed. Thanks again for the quick response! This add-on is great and going to help me considerably.
I like the idea of injecting the context via a service. Any downsides you see to that?
Yeah, this is kind of where I was trying to go with the session
. I've had a hard time recommending a specific way to deal with this because each case is so slightly different.
I implemented your solution with query for a nested route that doesn't use any session data.
Looks good :)
When I used this solution, it worked great but added the orgId as a Query String parameter to the end.
I have not experienced this. What version of ember-data are you using? I wonder if ember-data is appending your orgId
. One troubleshooting step you could do is to implement the orgId
urlSegment to delete its key, like this:
// adapter
export default ApplicationAdapter.extend(UrlTemplates, {
queryUrlTemplate: '{+host}/{+namespace}/orgs/{orgId}/locations',
urlSegments: {
orgId(type, id, snapshot, query) {
var orgId = query.orgId;
delete query.orgId;
return orgId;
},
},
});
This add-on is great and going to help me considerably.
I'm so glad to hear that; thank you!
@amiel, thanks again! Overriding orgId
and removing it from the query
worked great.
I am using Ember Data 2.3.3.
I currently have the following URL I am trying to get all photos belong to a user:
/users/:user_id/photos
I see in the wiki that it is possible to use URL Segments for
createRecordUrlTemplate
using snapshots to get the ID. However, what is the best way to handle building the path forfindAllUrlTemplate
to get the ID for theparent
akausers
?In the URL Segments, the snapshot will be null for
findAll
andquery
.