findAll always sends a request to the server


#1

I am running into a weird situation where my model’s findAll store queries are always sending a request to the server even though the data for the specific model should already be cached. This is what I was trying:

this.store.findAll('devices/lock')

However, I always saw a network request to the server whenever I would navigate to the route that loads this model.

I then found information that findAll will try to do a background refresh of data after initially resolving it if it is already in store. This is not the behavior that I would like to achieve, so I changed my code to the following:

this.store.findAll('devices/lock', {backgroundReload: false, reload: false})

Unfortunately, this does not seem to work either, the requests are still always sent to the server when I navigate to this route. I have even try to add the following to my RESTAdapter:

shouldReloadRecord: function (store, ticketSnapshot) {
    return false;
},
shouldBackgroundReloadRecord: function (store, snapshot) {
    return false;
}

Does anybody know what the issue could be? Any help is greatly appreciated!


#2

Forgot to mention, I am working with Ember 2.0.1 and Ember-Data 2.0.1


#3

Use store.peek and store.peekAll

The conceptual difference between peek and find is that peek will filter records that are already in the store while find will always hit the server.


#4

This sounds like a bug, and I vaguely recall running across this myself. I would open an issue if one already isn’t. For now, you can use the peek methods that @christophermilne mentioned.

Step through this method to see where the problem is: https://github.com/emberjs/data/blob/v2.0.1/packages/ember-data/lib/system/store.js#L1009-L1029


#5

To change the loading behavior of store.findAll() you can use the shouldReloadAll() and shouldBackgroundReloadAll() hooks.

shouldReloadRecord() and shouldBackgroundReloadRecord() is for the store.findRecord() call.


#6

@christophermilne unfortunately those two methods do not work for me, because I need to get the records once from the server. I could do a findAll and then if exists do a peekAll most likely, but it seems like there should be a better option.

@wecc that worked, thanks! Seems like a hacky solution for just wanting to do this for certain models.

@jasonmit I stepped through that method. It seems like the code should be a little different than it is.

Line 939:

if (adapter.shouldBackgroundReloadAll(this, snapshotArray)) {
  promiseArray(_findAll(adapter, this, typeClass, sinceToken, options));
}

I feel like there should be a return in there and also the if statement should check if we are disabling background reload from the options. Is this inconsistent with what we expect (I feel like it is) and if so, where should I report this?


#7

Create an issue in ember-data repository to gather feedback.