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:
- Create a new store using the fixture adapter and attach it to your app.
- Define your model using
DS.Model.extend
. - 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.Documenter = 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.Documenter.reopenClass({
FIXTURES: [
{ id: 1, firstName: 'Trek', lastName: 'Glowacki' },
{ id: 2, firstName: 'Tom' , lastName: 'Dale' }
]
});
That's it! You can now use all of methods for Finding Records in your application. For example:
App.DocumenterRoute = 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.