Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 39 ยท Ember Data and Models

Ember Data: Models and the Store

Meet Ember Data, the library that turns API responses into cached records - the store, models, the identity map and how routes load records - with a runnable stand-in store.

Most apps show data that lives on a server: posts, products, users, orders. You could call fetch in every route and component, but you would soon face the same questions again and again. Where do I keep what I loaded? How do I avoid loading the same user five times? How do I update a name and have every screen that shows it update too? Ember Data answers these with a store that holds records described by models.

Real versus stand-in. Ember Data is not available on this site, so the model and route code is real, read-only Ember. The runnable block builds a tiny store in plain JavaScript that behaves like the real one in the ways this lesson explains (an identity map, a cache, one record per id). It is a teaching model, not the library.

The pieces

flowchart LR
  R["Route / component"] -- "store.findRecord('post', 1)" --> S["Store (cache of records)"]
  S -- "cache miss: request" --> A["Adapter (URL, headers)"]
  A -- "HTTP" --> API["Your API"]
  API -- "JSON" --> Z["Serializer (JSON to records)"]
  Z --> S
  S -- "record" --> R

A record request goes through the store, the adapter and the serializer.

  • Model: a class describing a type of record and its fields (post: title, body, author).
  • Record: one instance of a model, such as post 1. Records are reactive: change a field and templates update.
  • Store: a service holding all records, one per type and id. You ask it for records, create new ones and save changes.
  • Adapter: knows how to talk to the server (URLs, HTTP verbs, headers, authentication).
  • Serializer: converts the server's JSON into the shape the store expects, and back when saving.

By default Ember Data speaks the JSON:API format, but adapters and serializers can be changed to fit REST-like or any other API (see Adapters and Serializers).

A first model

JavaScript
// app/models/post.js
import Model, { attr } from '@ember-data/model';

export default class PostModel extends Model {
  @attr('string') title;
  @attr('string') body;
  @attr('date') publishedAt;
}

The file name post.js gives the model name post. A model is just a class whose @attr fields are tracked automatically. Generate one with ember generate model post title:string body:string.

The server's response for GET /posts/1 is expected to look like this (JSON:API):

JSON
{
  "data": {
    "type": "posts",
    "id": "1",
    "attributes": {
      "title": "Hello Ember",
      "body": "A first post.",
      "published-at": "2026-03-01T09:00:00Z"
    }
  }
}

Ember Data maps published-at to publishedAt and the date string to a Date for you.

Asking the store for records

The store is a service. Inject it with @service and call its methods:

JavaScript
// app/routes/posts.js
import Route from '@ember/routing/route';
import { service } from '@ember/service';

export default class PostsRoute extends Route {
  @service store;

  model() {
    return this.store.findAll('post');
  }
}
JavaScript
// app/routes/post.js
export default class PostRoute extends Route {
  @service store;

  model({ post_id }) {
    return this.store.findRecord('post', post_id);
  }
}

The route returns a promise; the router waits for it, then renders the template (see Route Classes and the Model Hook). The template reads the record like any object:

JavaScript
<template>
  <h1>{{@model.title}}</h1>
  <p>{{@model.body}}</p>
</template>

The main finder methods:

Method What it does
findAll('post') Loads all posts (and returns the cached ones immediately while refreshing in the background).
findRecord('post', id) Loads one post by id.
query('post', { filter }) Asks the server for a filtered list; the result is not kept as a live "all posts".
peekAll('post') / peekRecord('post', id) Reads from the cache only; never touches the network. Returns what is already loaded.
createRecord('post', {...}) Creates a new record locally.

The identity map: one record per id

The most important idea in Ember Data is that the store keeps one record object for each (type, id). Load post 1 on the list page and again on the detail page and you get the same object. Edit its title in a form, and the list, the page header and a sidebar all update, because they all point at one record.

JS Stand-in: an identity map

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real findRecord goes further: by default it returns the cached record immediately and refreshes it in the background if it might be stale (backgroundReload). You can control this with options such as { reload: true } (always wait for the server) or { backgroundReload: false }.

Record state

Each record knows its own condition. Useful properties for templates and actions:

  • record.isNew: created locally, not saved yet.
  • record.hasDirtyAttributes: some attribute differs from the last saved or loaded value.
  • record.isSaving: a save is in flight.
  • record.isDeleted: marked for deletion.
  • record.isLoaded, record.isError: loading state and failed state.

These are all tracked, so a template like {{#if @model.hasDirtyAttributes}}<p>Unsaved changes</p>{{/if}} just works.

When to use Ember Data

Ember Data pays off when your app has many screens sharing the same entities, relationships between them (see Relationships), editing flows and an API that follows a convention. It adds a learning curve and some weight, so for a handful of one-off requests a plain fetch in a route (or a service) is perfectly fine. The two can live side by side in one app.

Common mistakes

  • Forgetting to inject the store. In a route or component you need @service store; before using this.store.
  • Using peekAll as if it loaded data. It only reads the cache; call findAll first.
  • Mutating a record's data from many places. Because records are shared, a change anywhere shows everywhere; decide who owns edits.
  • A mismatching model name. findRecord('Post', 1) fails; model names are lowercase, dasherized (blog-post).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the identity map in Ember Data?

    Because there is one object per record, changes show consistently on every screen that uses it.
  2. 2. Which store method reads only from the cache and never makes a request?

    peekRecord and peekAll return what is already loaded and do not touch the network.
  3. 3. Where does the knowledge of URLs and HTTP headers live?

    Adapters talk to the server; serializers translate the payloads; models describe fields.
  4. 4. What does record.hasDirtyAttributes tell you?

    It is true when a field differs from its last loaded or saved value, which is useful for "unsaved changes" warnings.

Summary

  • Ember Data = models (shape) + store (cache) + adapter (HTTP) + serializer (JSON conversion).
  • Inject the store with @service store and use findAll, findRecord, query and peek*.
  • The identity map gives one shared record per type and id.
  • Records expose tracked state such as isNew and hasDirtyAttributes.
  • It is optional: use it when many screens share entities and relationships.

Next: Models, Attributes and Transforms