home
  • Blog
2.5
  • Getting Started
  • Tutorial
  • The Object Model
  • Routing
  • Templates
  • Components
  • Controllers
  • Models
    • Introduction
    • Defining Models
    • Finding Records
    • Creating, Updating and Deleting
    • Relationships
    • Pushing Records into the Store
    • Handling Metadata
    • Customizing Adapters
    • Customizing Serializers
  • Application Concerns
  • Testing
  • Ember Inspector
  • Addons and Dependencies
  • Configuring Ember.js
  • Contributing to Ember.js
  • Glossary
Old Guides - You are viewing the guides for Ember v2.5.0.
Go to v5.0.0

Handling Metadata

Edit pencil

Along with the records returned from your store, you'll likely need to handle some kind of metadata. Metadata is data that goes along with a specific model or type instead of a record.

Pagination is a common example of using metadata. Imagine a blog with far more posts than you can display at once. You might query it like so:

let result = this.store.query('post', {
  limit: 10,
  offset: 0
});

To get different pages of data, you'd simply change your offset in increments of 10. So far, so good. But how do you know how many pages of data you have? Your server would need to return the total number of records as a piece of metadata.

Each serializer will expect the metadata to be returned differently. For example, Ember Data's JSON deserializer looks for a meta key:

{
  "post": {
    "id": 1,
    "title": "Progressive Enhancement is Dead",
    "comments": ["1", "2"],
    "links": {
      "user": "/people/tomdale"
    },
    // ...
  },

  "meta": {
    "total": 100
  }
}

Regardless of the serializer used, this metadata is extracted from the response. You can then read it with .get('meta').

This can be done on the result of a store.query() call:

store.query('post').then((result) => {
  let meta = result.get('meta');
})

On a belongsTo relationship:

let post = store.peekRecord('post', 1);

post.get('author').then((author) => {
  let meta = author.get('meta');
});

Or on a hasMany relationship:

let post = store.peekRecord('post', 1);

post.get('comments').then((comments) => {
  let meta = comments.get('meta');
});

After reading it, meta.total can be used to calculate how many pages of posts you'll have.

To customize metadata extraction, check out the documentation for your serializer.

left arrow
Pushing Records into the Store
Customizing Adapters
right arrow
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