Comments (4)
@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.
from ember-data-url-templates.
@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 found this shortly before I saw your response - http://blog.ksol.fr/ember-data-working-with-nested-api-resources/. I like the idea of injecting the context via a service. Any downsides you see to that?
-
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.
from ember-data-url-templates.
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!
from ember-data-url-templates.
@amiel, thanks again! Overriding orgId
and removing it from the query
worked great.
I am using Ember Data 2.3.3.
from ember-data-url-templates.
Related Issues (20)
- IDs are double URI-encoded HOT 3
- Return custom adapter URL outside adapter HOT 1
- Nested query objects not working HOT 11
- query keys in query string HOT 11
- Can I inject i18n in my adapters? HOT 1
- Forward slash is being encoded HOT 2
- Arrays not working right HOT 10
- namespace defined in ApplicationAdapter is not respected HOT 2
- Update documentation to include `adapterOptions` example
- Conflict between ember-data-url-templates and Route's error action HOT 9
- Using URL templates in serializer links HOT 6
- Fastboot Support HOT 3
- Adapter tests not returning proper URLs HOT 2
- What do I do if my create and update api is different? HOT 2
- url-template for save method HOT 2
- Blog links in the Wiki are broken HOT 1
- Custom urlSegments fails de build URL HOT 1
- findRecord(id) uses queryRecordUrlTemplate, not findRecordUrlTemplate HOT 5
- Array params have indexes now HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-data-url-templates.