Spezify the model at findRecords/query?

Hej Ember Community :upside_down_face:, I am trying to get data from by backend in ember. The Data is represented as an array. So i need to use one of the methods thats return an DS.PromiseArray.

I am using currently for that this code:

  myStore.query('activeclassinstances', {}).then(function(activeclassinstances) {
    activeclassinstances.forEach(function(element) {
      //dosomething
    });

My problem is now, that my backend give me other data if i use a path like this: activeclassinstances/5 This data is still an array, so i cant use the other methods.

if i try something like this:

 myStore.query('activeclassinstances'/5, {}).then(function(activeclassinstances) {
        activeclassinstances.forEach(function(element) {
          //dosomething
        });

then he cant match the model. is there a option to spezify the model for a query somewhere different? or other way around that you spezify the url different. so the model has not the same name like the url ?

Hi @FlorianKrippner, could share examples of the backend responses to /activeclassinstances and /activeclassinstances/5? it might be a little easier to make good recommendations then. It almost sounds like you may not want to use Ember Data here, but it’s a little hard to tell.

1 Like

for this /activeclassinstances/5 its something like this:

{"data":[{"type":"activeclassinstances","id":"dashboard-extension-d143d4e2-a7ed-4d59-9c90-3c9f19294d15-8","attributes":{"landscapeID":"landscape-ecf20bcf-f242-4077-abcf-bbaea8acbf30-73","className":"SQLStatementHandler","instances":4}},{"type":"activeclassinstances","id":"dashboard-extension-d143d4e2-a7ed-4d59-9c90-3c9f19294d15-7","attributes":{"landscapeID":"landscape-ecf20bcf-f242-4077-abcf-bbaea8acbf30-73","className":"SQLConnectionHandler","instances":3}},{"type":"activeclassinstances","id":"dashboard-extension-d143d4e2-a7ed-4d59-9c90-3c9f19294d15-11","attributes":{"landscapeID":"landscape-ecf20bcf-f242-4077-abcf-bbaea8acbf30-73","className":"execute(String)","instances":2}},{"type":"activeclassinstances","id":"dashboard-extension-d143d4e2-a7ed-4d59-9c90-3c9f19294d15-9","attributes":{"landscapeID":"landscape-ecf20bcf-f242-4077-abcf-bbaea8acbf30-73","className":"setString(int, String)","instances":2}},{"type":"activeclassinstances","id":"dashboard-extension-d143d4e2-a7ed-4d59-9c90-3c9f19294d15-14","attributes":{"landscapeID":"landscape-ecf20bcf-f242-4077-abcf-bbaea8acbf30-73","className":"prepareStatement(String)","instances":1}}]}

and the /activeclassinstances is like the same, just unsorted and the array is not limited to 5

Ah ok… so typically the way this would work in a “vanilla” scenario is like this:

myStore.query('activeclassinstances', {perPage: 5, sort: "whatever"})

Which would generate the request: GET <host>/activeclassinstances?perPage=5&sort=whatever

And if you’re actually passing {} to the query you’d just use findAll instead (should result in the same AJAX request).

However if your backend expects “paging” syntax using slashes instead of regular query params you could override one or more adapter methods to generate the correct request for you. For example you could probably start with just overriding urlForQuery:


import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
  urlForQuery (query, modelName) {
    let url = this._super(...arguments);
    if(query.perPage) {
      url = `${url}/${perPage}`;
      delete query.perPage;
    }
    return url;
  }
});

Then you could call it like this:

myStore.query('activeclassinstances', { perPage: 5 })

and it would generate GET <host>/activeclassinstances/5

1 Like

ty. that was the answer i was looking for :heart: i will try it now out.