Transcript

Hey and welcome back to our series on Ember data. In the last episode, we talked about findAll and findRecord , two methods that help you get data from the server. In this episode, we’ll be talking about peekAll and peekRecord .

So I’m going to start off by showing you peekAll in action, and then we’ll get more into the details. Here we have a route which does this.store.peekAll('monster') . Notice the similarities between this.store.findAll('monster') .

Alright, so this is our route. Currently there are no monsters loaded yet, but here we’re going to go to the monsters route, and it’s going to load all the monsters. And then we go back to the peek route, it’ll have all the monsters that we had on here. This is because peekAll , it never actually hits the server. It only finds all the records that you currently have in your data store.

In addition to peekAll , you also have peekRecord , which is once again like findRecord , except it never hits your backend. It only takes stuff that’s already in the data store.

In addition to find and peek , there’s a third set of records starting with unload , so unloadAll and unloadRecord . Using these we can play around with what’s in the data store.

So we’ll start by loading all our monsters and then going to peek , and then using Ember dev tools, we’ll be able to access the store from the console and call, let’s say unloadAll , for monster. As you can see, it will take all of the monsters out of the data store. It will unload all of them.

You can also unload individual records, although it’s a little bit asymmetrical with how the other record methods work, since you first have to find that record and have a PR into the documentation to update this to findRecord , and then you call unloadRecord on the store and feed it the actual record instead of the type and the ID.

Of course using unload isn’t without its problems. For example if we go to the monsters route again, then they’ll load but it gives us these errors, something about destroyed object. You’ll also note it didn’t actually take us to the monsters route. We’re still on monsters peek, even though all the monsters have loaded again. We click again, we’re there, and we see that there are problems with the current team. So one of the ways in which we’ve loaded the current team has created problems with its interaction with unloadAll . So that’s something to be aware of. It can cause problems.

So that’s the peek methods and the unload methods. Before I sign off for today, I want to show you a little bit more, a little bit deeper about how stuff is handled with the store.

Here’s another method, recordIsLoaded . And what this does, it basically tells you whether a record is loaded or not. Now this method is basically... it’s defined and never used except to test it, but recordIsLoaded does call hasRecordForId , which is used in a lot of places, including in things like findRecord to check whether the record is already there. It’s used fairly transparently in peekRecord . It checks whether the ID is there, and if it is then it returns an internalModel for that ID, so basically getting it from the store. Otherwise, it’ll return null .

For findRecord , it’s a little more complicated, but basically it will... if it is there then it will return the record that is in the store. If it’s not there, then it will find it from the server. You can overwrite this by doing findRecord and passing in the option {reload: true} . And this will always try to find the record from the server even if it has one in the cache. So it might get it from the cache but then replace it with one from the server.

So in this episode we’ve gone over six different methods on the Ember data store, peekAll and peekRecord , unloadAll and unloadRecord , and recordIsLoaded and hasRecordForId . These should help you understand more about how the caching works on the store and how you can manipulate that by hand.