1
votes

I am trying to understand how can I force ember store to make another api call instead of fetching records from cache when I use findAll. I know if records are already loaded then ember returns cached data unless asked to skip it. On doing some study (blogs, documentation and stack overflow), people has suggested to use reload: true flag with findAll but its not working for me. When I do a reload:true flag then store still returns me data from the cache.

If I am missing anything, then please help.

Code I have:

fetchStudentData() {
   this.get('store').findAll('student').then((response) => {
         return response.data;
   });
}

This function is tied to a button so on clicking I need to re-initiate the API call. I replaced the store call to use:

this.get('store').findAll('student', { reload: true }).then((response) => {
         return response.data;
   });

But this also didnt help as it still returned me old records in the store cache.

1
what code do you have at the moment? - NullVoxPopuli
Let me update the post with the code I have. - Bhavya Bansal
@NullVoxPopuli any comments? - Bhavya Bansal
added answer, lemme know how it goes! - NullVoxPopuli
Could you describe your use case a little bit more in detail? Why do you want to enforce the reload? Is it about getting rid of records in store that are already deleted on API? As far as I know this.store.findAll('my-model', { reload: true }) will enforce a reload but that will only update existing and add new records. It won't remove any record that is already in local store but not returned by API. Is this the issue you are facing? - jelhan

1 Answers

0
votes

I'm not familiar with the issue you are having, but if you don't care about your cache at all, you could do this:

async myMethod() {
  this.store.unloadAll('student');
  let students = await this.store.findAll('student');
}

Something that puzzled me about your query is that you're doing response.data inside your then, I know data is an attribute on the raw json payload, but all the store methods that return values return Models. So, you'll get student instances back instead of raw json (if you want raw json (I recommend against this), you can just use fetch).