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.

当开发客户端应用时,服务端可能还没有完成对应API的开发。使用FixtureAdapter可以先进行Ember应用开发,然后切换到其他的适配器来使用API,并且这个切换并不需要改变应用的代码。

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.

  4. 创建一个新的、使用夹具适配器的store并将其关联到应用。

  5. 使用DS.Model.extend来定义模型。
  6. 将夹具(样本数据)关联到对应的模型类。

Creating a Fixture Adapter

创建夹具适配器

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

只需将其定义为Ember.Application应用的ApplicationAdapter属性即可。

  1. var App = Ember.Application.create();
  2. 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.

查看定义模型一章可以获取更多详细的关于定义模型的介绍,作为简单的示例,这里定义一个编写Ember文档的人员的模型。

  1. App.Documenter = DS.Model.extend({
  2. firstName: DS.attr( 'string' ),
  3. lastName: DS.attr( 'string' )
  4. });

Attach Fixtures to the Model Class

关联夹具到模型类

Attaching fixtures couldn’t be simpler. Just attach a collection of plain JavaScript objects to your Model’s class under the FIXTURES property:

关联夹具非常之简单。只需要将一个Javascript对象集合赋值给模型的FIXTURES属性即可:

  1. App.Documenter.FIXTURES = [
  2. { id: 1, firstName: 'Trek', lastName: 'Glowacki' },
  3. { id: 2, firstName: 'Tom' , lastName: 'Dale' }
  4. ];

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

这就是所有的配置过程了,现在便可以施工用查找记录一章中得方法了。例如:

  1. App.DocumenterRoute = Ember.Route.extend({
  2. model: function() {
  3. return this.store.find('documenter', 1); // returns a promise that will resolve
  4. // with the record representing Trek Glowacki
  5. }
  6. });

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.

REST Adapter不同,夹具适配器并不做任何命名惯例的猜测。如同上例中所示,如果在DS.Model.extend中定义了一个firstName属性,那么需要在夹具样本数据中使用firstName来表示该属性。

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.

更重要地是,需要确保夹具样本数据中得每一条记录都有一个唯一的标识。缺省情况下,Ember Data假定该标识为id。如果没有重定义主键标识名,又未在记录中提供一个id,那么夹具适配器会抛出一个错误。