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


#1

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.


#2

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


#3

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


#4

It’s attempting to resolve asynchronous relationships.

http://thau.me/2014/09/ember-data-mastering-async-relationships/


#5

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?


#6

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. http://zoltan.nz/bookstore-client/

Payload:

Request:

However in this case I use query because of the params. https://github.com/szines/bookstore-client/blob/master/app/routes/books.js#L11-L13

In the Rails Api, there is an include, which inserts all the connected models: https://github.com/szines/bookstore-api/blob/master/app/controllers/books_controller.rb#L9

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

Payload:

Requests:


#7

In terms of findAll() caching, you can read more about it here: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_better-caching-defaults-for-code-findall-code-and-code-findrecord-code

There is a nice tutorial and implementation as well on Frank’s blog: http://emberigniter.com/building-user-interface-around-ember-data-app/#the-author-page-ought-to-be-fast:6c3628d73a23ec0b1761f2608833a52f


#8

You could extend your adapter and override:

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

?

Basically the opposite of this ( http://emberigniter.com/force-store-reload-data-api-backend ), the adapter API is kinda explained there.


#9

Thanks Frank! This was the reason. Great post!