Accessing non-REST path


#1

Hi,

I’m trying to find how to call a path like /posts/mine (knowing that I also have stuff like /posts/1) with ember-data. This is completely outside of the standard REST model, so I don’t know how I can access it… Should I consider mine like a post ID?

Thanks for your help!


#2

@frsechet if /posts/mine returns a singular post model just like /posts/1 I’d think that would work just fine. I could be wrong but I don’t think ED will actually assert that the id param matches the returned record ID. So I think you could just say `store.findRecord(‘post’, ‘mine’) and it would work fine. Worth a shot. If not you could probably play around with the adapter to get it to do what you want (I’m not really sure from your description what the use case is).

In our app we have a possibly similar sort of endpoint, /users/me which returns the currently logged in user. Because of some subtle differences in the way the server handles that request we actually just opted to do a non-Ember-Data AJAX request and then load the record into the store manually once we’d received it and munged it a little bit. So all that to say that would be another option.


#3

Sorry for not being explicit enough :slight_smile: The backend service /posts/mine returns all the posts by the currently logged user, so it’s an array of post objects, whilst /posts/1 returns just one post.

I think I’ll do a second model, with a custom namespace and path and it should work…


#4

Ah that makes sense. In that case I’d still recommend using the adapter vs duplicating the model. You can do something like this to add the extra path param to a findAll (if passed):

// /adapters/post.js
...
  urlForFindAll(id, modelName, snapshot) {
    let url = this._super(...arguments);
    let extraPath = Ember.get(snapshot, 'adapterOptions.extraPath');
    if (extraPath) {
      url += '/' + extraPath; // assumes no query params are present, which is pretty safe for a findAll
    }
    return url;
  }
...

And then to actually use it:

this.store.findAll("post", {adapterOptions: {extraPath: "mine"}})

And of course if you don’t pass adapterOptions any findAll calls on the post model will work like normal.