home
  • Blog
1.12
  • Getting Started
  • Concepts
  • The Object Model
  • Application
  • Templates
  • Routing
  • Components
  • Controllers
  • Models
    • Introduction
    • Defining Models
    • Creating and Deleting Records
    • Pushing Records into the Store
    • Persisting Records
    • Finding Records
    • Working with Records
    • The Rest Adapter
    • Connecting to an HTTP Server
    • Handling Metadata
    • Customizing Adapters
    • Frequently Asked Questions
  • Views
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Ember Inspector
  • Cookbook
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.12.0.
Go to v5.0.0

Finding Records

Edit pencil

The Ember Data store provides a simple interface for finding records of a single type through the store object's find method. Internally, the store uses find, findAll, and findQuery based on the supplied arguments.

The first argument to store.find() is always the record type. The optional second argument determines if a request is made for all records, a single record, or a query.

Finding All Records of a Type

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

To get a list of records already loaded into the store, without making another network request, use all instead.

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

find returns a DS.PromiseArray that fulfills to a DS.RecordArray and all directly returns a DS.RecordArray.

It's important to note that DS.RecordArray is not a JavaScript array. It is an object that implements Ember.Enumerable. This is important because, for example, if you want to retrieve records by index, the [] notation will not work--you'll have to use objectAt(index) instead.

Finding a Single Record

If you provide a number or string as the second argument to store.find(), Ember Data will assume that you are passing in an ID and attempt to retrieve a record of the type passed in as the first argument with that ID. This will return a promise that fulfills with the requested record:

var aSinglePost = this.store.find('post', 1); // => GET /posts/1

Querying For Records

If you provide a plain object as the second argument to find, Ember Data will make a GET request with the object serialized as query params. This method returns DS.PromiseArray in the same way as find with no second argument.

For example, we could search for all person models who have the name of Peter:

var peters = this.store.find('person', { name: "Peter" }); // => GET to /persons?name=Peter

Integrating with the Route's Model Hook

As discussed in Specifying a Route's Model, routes are responsible for telling their template which model to render.

Ember.Route's model hook supports asynchronous values out-of-the-box. If you return a promise from the model hook, the router will wait until the promise has fulfilled to render the template.

This makes it easy to write apps with asynchronous data using Ember Data. Just return the requested record from the model hook, and let Ember deal with figuring out whether a network request is needed or not.

app/router.js
var Router = Ember.Router.extend({});

Router.map(function() {
  this.route('posts');
  this.route('post', { path: ':post_id' });
});

export default Router;
app/routes/posts.js
export default Ember.Route.extend({
  model: function() {
    return this.store.find('post');
  }
});
app/routes/post.js
export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('post', params.post_id);
  }
})
left arrow
Persisting Records
Working with Records
right arrow
On this page

  • Finding All Records of a Type
  • Finding a Single Record
  • Querying For Records
  • Integrating with the Route's Model Hook
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue