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

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:

var result = this.store.find("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.

By default, 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
  }
}

The metadata for a specific type is then set to the contents of meta. You can access it either with store.metadataFor, which is updated any time any query is made against the same type:

var meta = this.store.metadataFor("post");

Or you can access the metadata just for this query:

var meta = result.get("content.meta");

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

You can also customize metadata extraction by overriding the extractMeta method. For example, if instead of a meta object, your server simply returned:

{
  "post": [
    // ...
  ],
  "total": 100
}

You could extract it like so:

app/serializers/application.js
export default DS.RESTSerializer.extend({
  extractMeta: function(store, type, payload) {
    if (payload && payload.total) {
      store.setMetadataFor(type, { total: payload.total });  // sets the metadata for "post"
      delete payload.total;  // keeps ember data from trying to parse "total" as a record
    }
  }
});
left arrow
Connecting to an HTTP Server
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