Giter Site home page Giter Site logo

Comments (4)

amiel avatar amiel commented on June 15, 2024

@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.

borwahs avatar borwahs commented on June 15, 2024

@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.

  1. 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?

  2. 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.

amiel avatar amiel commented on June 15, 2024

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.

borwahs avatar borwahs commented on June 15, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.