If your Ember application needs to load JSON data from an HTTP server, this guide will walk you through the process of configuring Ember Data to load records in whatever format your server returns.

如果Ember应用需要从HTTP服务器加载JSON数据,本指南将介绍如何配置Ember Data来从服务器端加载记录,不论服务器返回的数据格式是什么样子。

The store uses an object called an adapter to know how to communicate over the network. By default, the store will use DS.RESTAdapter, an adapter that communicates with an HTTP server by transmitting JSON via XHR.

仓库使用了一个称为适配器,知道如何通过网络进行通信的对象。默认情况下,仓库会使用DS.RESTAdapter,一个可以与HTTP服务器通过XHR进行JSON数据交互的适配器。

This guide is divided into two sections. The first section covers what the default behavior of the adapter is, including what URLs it will request records from and what format it expects the JSON to be in.

本指南分为两部分。第一部分介绍适配器的默认行为,包括将从哪些URL获取记录,和期望的JSON格式。

The second section covers how to override these default settings to customize things like which URLs data is requested from and how the JSON data is structured.

第二部分介绍如何重新定义这些默认行为,如请求数据的URL和JSON的结构。

URL Conventions

URL约定

The REST adapter uses the name of the model to determine what URL to send JSON to.

REST适配器使用模型的名称来判定将要发送JSON的URL。

For example, if you ask for an App.Photo record by ID:

例如,用ID来请求一个App.Photo的记录:

  1. App.PhotoRoute = Ember.Route.extend({
  2. model: function(params) {
  3. return this.store.find('photo', params.photo_id);
  4. }
  5. });

The REST adapter will automatically send a GET request to /photos/1.

REST适配器将发送一个GET请求到/photos/1

The actions you can take on a record map onto the following URLs in the REST adapter:

可以在记录上执行的操作,在REST适配器中被映射到如下的URL上:

操作HTTP VerbURL
查询GET/people/123
查询所有GET/people
更新PUT/people/123
创建POST/people
删除DELETE/people/123

JSON Conventions

JSON约定

Given the following models:

给定如下模型:

  1. var attr = DS.attr,
  2. hasMany = DS.hasMany,
  3. belongsTo = DS.belongsTo;
  4. App.Post = DS.Model.extend({
  5. title: attr(),
  6. comments: hasMany('comment'),
  7. user: belongsTo('user')
  8. });
  9. App.Comment = DS.Model.extend({
  10. body: attr()
  11. });

Ember Data expects that a GET request to /posts/1 would return the JSON in the following format:

Ember Data期望一个发送到/posts/1请求将返回如下的JSON格式:

  1. {
  2. "post": {
  3. "id": 1,
  4. "title": "Rails is omakase",
  5. "comments": ["1", "2"],
  6. "user" : "dhh"
  7. },
  8. "comments": [{
  9. "id": "1",
  10. "body": "Rails is unagi"
  11. }, {
  12. "id": "2",
  13. "body": "Omakase O_o"
  14. }]
  15. }

Customizing the Adapter

自定义适配器

To customize the REST adapter, define a subclass of DS.RESTAdapter and name it App.ApplicationAdapter. You can then override its properties and methods to customize how records are retrieved and saved.

自定义REST适配器,需要定义一个DS.RESTAdapter的子类,并将其命名为App.ApplicationAdapter。接下来只需要重新定义它的属性和方法,就可以自定义记录如何加载和保存了。

Customizing URLs

自定义URL

URL Prefix

URL前缀

If your JSON API lives somewhere other than on the host root, you can set a prefix that will be added to all requests.

如果JSON API并不在主机的根目录,而是在一个其他的路径下,那么需要为请求设置一个URL前缀。

For example, if you are using a versioned JSON API, a request for a particular person might go to /api/v1/people/1.

例如,在使用了版本化的JSON API时,请求一个特定的person可能需要发送请求到/api/v1/people/1

In that case, set namespace property to api/v1.

这种情况下,需要设置namespace属性为api/v1

  1. App.ApplicationAdapter = DS.RESTAdapter.extend({
  2. namespace: 'api/v1'
  3. });

Requests for a person with ID 1 would now go to /api/v1/people/1.

现在请求一个ID为1person就会发送请求到/api/v1/people/1

URL Host

URL主机

If your JSON API runs on a different domain than the one serving your Ember app, you can change the host used to make HTTP requests.

如果JSON API的主机与提供Ember应用服务的主机不同,那么需要修改发送HTTP请求的主机。

Note that in order for this to work, you will need to be using a browser that supports CORS, and your server will need to be configured to send the correct CORS headers.

注意为了使其正常工作,需要使用支持CORS的浏览器,并且服务器需要配置支持发送正确的CORS头。

To change the host that requests are sent to, set the host property:

修改请求对应的目标主机,需要设置host属性:

  1. App.ApplicationAdapter = DS.RESTAdapter.extend({
  2. host: 'https://api.example.com'
  3. });

Requests for a person with ID 1 would now target https://api.example.com/people/1.

现在请求一个ID为1person就会发送请求到https://api.example.com/people/1

Custom HTTP Headers

自定义HTTP头

Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary headers can be set as key/value pairs on the RESTAdapter‘s headers property and Ember Data will send them along with each ajax request.

一些API需要指定HTTP头,例如提供一个API密钥。任意键值对HTTP头可以通过RESTAdapterheaders属性来进行设置,Ember Data会将这些头设置到到每个Ajax请求中去。

For Example

例如:

  1. App.ApplicationAdapter = DS.RESTAdapter.extend({
  2. headers: {
  3. "API_KEY": "secret key",
  4. "ANOTHER_HEADER": "Some header value"
  5. }
  6. });

Requests for any resource will include the following HTTP headers.

请求任意资源都会包含如下的HTTP头。

  1. ANOTHER_HEADER: Some header value
  2. API_KEY: secret key