2
votes

In the /photos route of my ember-app I can see that if I use the query function, the records does not come from local indexedDB:

the route has a loading template with a spinner;

if in the route I do:

model: function() {
    return this.store.findAll('photo');
}

the record are loaded only the first time; I see the spinner for a while and then the photo are shown; if I go on another route and then back to the photos route, the spinner is not shown, the data are taken from local indexedDB and a new request to the REST api is done in background;

but if I do

model: function() {
    return this.store.query('photo', {}});
}

I see the spinner not only the first time but every time I open the route; so I guess the data is fetched again

1
that is correct behaviour, because ember data cannot know if same query request return same set objects all the time so cannot cache itBek
Ok thanks for pointing this out... Is there any way to work around this to tell ember data to behave like findall? (caching and making new request in background?)Cereal Killer
I don't think it is possible you can build your own caching mechanism on top of ember-data query I thinkBek

1 Answers

2
votes

To clarify, ember-datas intention is to make sure the data thats consumed is as in sync with the server as possible.. thus calling findAll it will return you new data to make sure it hasnt changed. You can on the other hand employ methods such as findRecord, peekRecord and peekAll to have more control if you want this data from the cache or freshly loaded


peekRecord will load the data exclusively from the cache for one model. http://emberjs.com/api/data/classes/DS.Store.html#method_peekRecord


If you want the content to be loaded from the cache you want to use findRecord. If not loaded it will make a request, then subsequent requests will come from the cache.

store.findRecord('filter', 1) http://emberjs.com/api/data/classes/DS.Store.html#method_findRecord

I also recommend you use the ember inspector browser extension to see what models are loaded in the cache for more clarity.


If you want to see all the currently loaded content for a model you may use peekAll http://emberjs.com/api/data/classes/DS.Store.html#method_peekAll