home
  • Blog
1.11
  • 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
    • Using Fixtures
    • The Rest Adapter
    • Connecting to an HTTP Server
    • Handling Metadata
    • Customizing Adapters
    • Frequently Asked Questions
  • Views
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Cookbook
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.11.0.
Go to v5.0.0

Using Fixtures

Edit pencil

When developing client-side applications, your server may not have an API ready to develop against. The FixtureAdapter allows you to begin developing Ember.js apps now, and switch to another adapter when your API is ready to consume without any changes to your application code.

Getting Started

Using the fixture adapter entails three very simple setup steps:

  1. Create a new store using the fixture adapter and attach it to your app.
  2. Define your model using DS.Model.extend.
  3. Attach fixtures (also known as sample data) to the model's class.

Creating a Fixture Adapter

Simply attach it as the ApplicationAdapter property on your instance of Ember.Application:

var App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter;

Define Your Model

You should refer to Defining a Model for a more in-depth guide on using Ember Data Models, but for the purposes of demonstration we'll use an example modeling people who document Ember.js.

app/models/documenter.js
export default DS.Model.extend({
  firstName: DS.attr( 'string' ),
  lastName: DS.attr( 'string' )
});

Attach Fixtures to the Model Class

In order to attach fixtures to your model, you have to use reopenClass method and define fixtures:

app/models/documenter.js
var Documenter = DS.Model.extend({
  firstName: DS.attr( 'string' ),
  lastName: DS.attr( 'string' )
});

Documenter.reopenClass({
  FIXTURES: [
    { id: 1, firstName: 'Trek', lastName: 'Glowacki' },
    { id: 2, firstName: 'Tom' , lastName: 'Dale'     }
  ]
});

export default Documenter;

That's it! You can now use all of methods for Finding Records in your application. For example:

app/routes/documenter.js
export default Ember.Route.extend({
  model: function() {
    return this.store.find('documenter', 1); // returns a promise that will resolve
                                             // with the record representing Trek Glowacki
  }
});

Naming Conventions

Unlike the REST Adapter, the Fixture Adapter does not make any assumptions about the naming conventions of your model. As you saw in the example above, if you declare the attribute as firstName during DS.Model.extend, you use firstName to represent the same field in your fixture data.

Importantly, you should make sure that each record in your fixture data has a uniquely identifiable field. By default, Ember Data assumes this key is called id. Should you not provide an id field in your fixtures, or not override the primary key, the Fixture Adapter will throw an error.

left arrow
Working with Records
The Rest Adapter
right arrow
On this page

  • Getting Started
  • Creating a Fixture Adapter
  • Define Your Model
  • Attach Fixtures to the Model Class
  • Naming Conventions
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