Why does this.store.findAll('post') make multiple requests?

Why does this.store.findAll(‘post’) make multiple requests? I thought if you call findAll() once for a given model, it just pulls what is in the store in subsequent requests.

Do you have a belongsTo or hasMany relationship in your post model?

yes I do. Does that affect how findAll() behaves?

It’s attempting to resolve asynchronous relationships.

I should have been more clear. I meant, why is it making multiple requests to /posts? Each time I visit the route with a model hook that calls this.store.findAll('post'), it is making a request to /posts. Is that how findAll() is supposed to work or is it supposed to only make a request once and then subsequent calls just return a promise that resolves with what is in the store?

findAll() will download all posts, however if you use somewhere in your template a related model, for example post.author, than the author’s detail will be downloaded as well. If the associated model was included in the original json file, it will not send a new request to the server, if it wasn’t part of the original json payload, than emberjs will send a request to the server to get the details about the connected author.

You can see here, using JSONApi, the payload contains all the connected models as well, so only one request was sent to the server. Bookstore

Payload:

Request:

However in this case I use query because of the params. bookstore-client/books.js at master · zoltan-nz/bookstore-client · GitHub

In the Rails Api, there is an include, which inserts all the connected models: bookstore-api/books_controller.rb at master · zoltan-nz/bookstore-api · GitHub

If I delete include from the json render, Ember.js will generate more requests to fulfill the models.

Payload:

Requests:

In terms of findAll() caching, you can read more about it here: Ember Data v1.13 Released

There is a nice tutorial and implementation as well on Frank’s blog: Building a Bookstore App with Ember Data - Ember Igniter

1 Like

You could extend your adapter and override:

shouldBackgroundReloadAll: function(store, snapshot) {
  return false;  // it's true by default
},

?

Basically the opposite of this ( Force Ember Data to reload data from backend API - Ember Igniter ), the adapter API is kinda explained there.

2 Likes

Thanks Frank! This was the reason. Great post!

1 Like