###Update: Don’t worry about this,
store.find has been simplified, read Update your finds.
Store#find (inject as
this.store.find) is one of those methods in Ember-Data which
confuses people, especially since it behaves very different depending
on the parameters passed-in.
This article try to demystify
store.find and explain how it will
behave accordingly to the parameters.
Let’s have its definition on top and then use it as reference:
We can use
find to load a single record or a collection of records.
If you want to run the commands in the article, you can download the
following ember-cli application https://github.com/abuiles/borrowers and then
ember server it will proxy all the API request to a public
Scenario #1: Loading a collection
If we call
find only with a model’s name then it will make a request
to load a list of records of that type, the following is an example of
First, in case you are wondering what
$E is, I’m grabbing an
instance of the application router using the
If we want to send query parameters with our request, we can pass an object as second argument and every key on the object will be included as parameter:
In the previous request we asked
find to load all the articles,
sending as parameters the key
the result from
find is a
live array too. When called it will make
a request to the server and then the collection will be updated if
more records are added or removed from the store.
I’ve seen people using the private methods
We should never rely on private functions, they are marked private for
a reason. Although Igor Terzic
findQuery might be marked as private by mistake.
We can achieve the same result using the method
Scenario #2: Loading a single record
We can also use
find to load an specific record, to do that we’ll
only need to pass the record
id as second argument:
In the previous example we are loading the friend with id 15, the
store will only make a request to the server if the friend is not
available in the store, to understand this, let’s go to
http://localhost:4200/friends and then on the console try the following:
If we open our network tab, we’ll see that the store didn’t make any request this time, the reason is that we asked for a friend which was already loaded into the store.
Is important to mention that find, all and filter return promises, when testing on the browser’s console we don’t have to worry about it, but if we want to use the result in our application then we need to keep this in mind.