Series: Introduction to Ember Data 2.0

Ember Data 2.0-Store Manipulation with Peek, Unload, and More

Published on Dec 09, 2015

Last episode we saw findAll and findRecord, the two most common ways of getting data into the store. Oftentimes those will be enough, but Ember also provides several ways to manipulate the data store in a more fine-grained way.

In this advanced episode, we’ll explore the methods peekAll, peekRecord, unloadAll, unloadRecord, recordIsLoaded, and hasRecordForId.

If you’re just getting started with Ember Data, feel free to skip this episode and go straight to episode 099.



You can call peekAll in a very similar way to findAll:


The difference is that peekAll will never go to the server- it will only use what's in the store. The same is true with peekRecord:'monster', 1)

You can remove records from the store with the unload methods:

//remove all data from store
//remove all data of a specific type from the store'monster')
//remove a specific record from the store

You can ask if a record is in the datastore:'monster', 1) //=>boolean'monster', 1) //=>boolean

The difference between the two methods is that hasRecordForId can take a model object as well as a string with the model name for the first parameter. hasRecordForId is also used throughout the codebase, while recordIsLoaded is an outward-facing API.

Written with StackEdit.


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'monster'). Notice the similarities between'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.

In the next episode, we’ll show how to create and delete records using these methods as well as several methods on the model itself, including deleteRecord, destroyRecord, and save. I’ll see you then.

Introduction to Ember Data 2.0

Subscribe to our mailing list