2

If I do

this.get('store').findAll('model')

It will make a request to /models and it will expect for an array. On the other hand, if I do

this.get('store').find('model', my_id)

It will make a request to /models/my_id and it will expect for one record of my model. The method .query() will basically do a GET request with the parameters I pass like

this.get('store').query('model', {something: 'foo'})

It will make a request to /models?something=foo.

My question is the following:

Can I make something which mixes all the three methods above? I want to make a request to the path /models/foo and I want to expect one array.

I did .find('model', params.something), but it threw an assert exception saying that it was expecting for one model and not one array.

renno
  • 2,659
  • 2
  • 27
  • 58
  • have you tried a promise ? means you first do first one and then while it's ok then() do next ? and also have you checked Querying for Multiple Records and this.get('store').queryRecord() ? – Majid Sep 22 '16 at 01:41
  • with promisses I will do multiple queries at the backend. I don't wanna do that... thanks for the suggestion, though – renno Sep 22 '16 at 12:18

1 Answers1

4

I'm assuming you're using the RestAdapter and you only want to do this for one model. In which case you can just customise your adapter by modifying the methods of the BuildURLMixin class. So if you wanted to specify the /models/foo as the url for query then you could implement urlForQuery in your custom adapter like:

urlForQuery(query, modelName) {
  return '/models/foo';
},

EDIT: If you want complete control over your url you can customise buildUrl

buildURL(modelName, id, snapshot, requestType, query) {
  switch (requestType) {
    case 'query':
      return '/models/' + query.something;
    default:
      return this._super();
  }
},

Then you when you call:

 this.get('store').find('model', {something: 'foo'})

it will hit /models/foo with no querystring.

EDIT2: Turns out the querystring gets appended way later than I thought. In fact to do what you want you're going to have to customise the query method of your adapter. If you look at the source for the RestAdapter you can see it adds the query at the point it calls the url:

return this.ajax(url, 'GET', { data: query });

So you can customise it to just:

return this.ajax(url, 'GET', { });

but you should be aware of the knock on effects i.e. no querystrings being applied at all

Adam Cooper
  • 8,077
  • 2
  • 33
  • 51
  • I tried that, but it messes with my path and the query keeps showing up `?something=foo` at the end. – renno Sep 22 '16 at 12:48
  • 1
    Yes it will add any query params you pass to it. To be honest I'm not following what you're trying to achieve here but you can use buildUrl if you want complete control over the url, I've updated my answer – Adam Cooper Sep 22 '16 at 13:02
  • This almost did the trick. When I did `this.get('store').query('model', {something: 'foo'})` the url became /models/foo?something=foo. I tried to use `.find()` but it threw an error saying that I should use `.query()`, because find does not accept a query object... – renno Sep 22 '16 at 13:41
  • 2
    Turns out buildUrl is too early in the pipeline, I've updated my answer again. Hopefully this time I've actually nailed it – Adam Cooper Sep 22 '16 at 15:04
  • You did. Thanks. I'm just starting with ember and this helped me a lot to understand more of its structure – renno Sep 22 '16 at 15:42
  • 1
    Cheers for the answer, was very helpful just now. – Oisín Foley May 14 '19 at 10:08