Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use in practice findAll and peekAll in Ember?

From EmberJS documentation i get the following two ways to retrieve all records of a given type, one that makes a request and one that doesn't.

var posts = this.store.findAll('post'); // => GET /posts

var posts = this.store.peekAll('post'); // => no network request

It seems to me that i always need to do first a findAll but isn't clear for my understanding when should i do a peekAll.

For example, the user enters my blog and then i get all the posts using findAll, then at some point in the same flow i need all those post, so i should use a peekAll to save bandwidth. So how should i know that i have requested all posts previously ? Should i save some global state to handle that ? I would assume that the first time the client request a peekAll if there isn't any record it will automatically do a findAll or maybe i should that manually but it probably introduce some boilerplate.

How do you use in practice findAll and peekAll or they equivalents for single record ? Any recommendation ?

like image 895
Javier Cadiz Avatar asked Feb 09 '23 03:02

Javier Cadiz


1 Answers

.findAll is cached:

First time store.find is called, fetch new data

Next time return cached data

Fetch new data in the background and update

This is the behavior of the new findRecord and findAll methods.

As you can read in Ember Data v1.13 blog post.

So, taking your example:

var posts = this.store.findAll('post'); // => GET /posts
// /\ or load from cache and update data in background /\

var posts = this.store.peekAll('post'); // => no network request

And:

It seems to me that i always need to do first a findAll but isn't clear for my understanding when should i do a peekAll.

Yes, you need to do first .findAll, but you are encouraged to use .findAll in all places, as it is cached and suited for multiple requests for data (from many places across application without wasting bandwidth).

For example, the user enters my blog and then i get all the posts using findAll, then at some point in the same flow i need all those post, so i should use a peekAll to save bandwidth. So how should i know that i have requested all posts previously ? Should i save some global state to handle that ? I would assume that the first time the client request a peekAll if there isn't any record it will automatically do a findAll or maybe i should that manually but it probably introduce some boilerplate.

I think user needs to have always up to date data in your application. What if you add blog post while he is browsing page? If you would use .peekAll() then user would need to refresh page to get latest data.

If you would like to save bandwidth then I would recommend you to implement maybe some kind of additional logic in Ember Adapter, but you have to find way to balance user requests with need to always serve latest data. You can do this by overriding Adapter's methods:

shouldReloadAll: function(store, snapshotRecordArray)
shouldBackgroundReloadAll: function(store, snapshotRecordArray)

See more info about these methods in Ember API docs.

How do you use in practice findAll and peekAll or they equivalents for single record ? Any recommendation ?

If you are completely sure that you always have up to date data after first request then use .peekAll. There is data can be always up to date, because, for example it almost never changes in your database. It depends however what are your needs and how did you design your data models. It's hard to find good example, but maybe imagine if you would have some models which contain only constants. Like PI value etc. Maybe you have imported it from somewhere and it is complete, closed set of something that will never change. Then, after first .findAll, (for example if it's core function to your application it could be defined in Application route beforeModel hook) you would be sure that no more requests are needed and you have all data.

You could also use .peekAll if your application would have something like Offline Mode and can rely only on data you already have.

like image 142
Daniel Kmak Avatar answered Mar 06 '23 19:03

Daniel Kmak