7.9 Models--Connection to an HTTP Server

Source: Internet
Author: User

I. Overview

1. If your ember application needs to load JSON data from an HTTP server, the process of configuring Emberdata will load records in any format returned by your server.

2. Store uses an object called adapter to learn how to communicate with the network. By default, the store will use DS. Restadapter, which is a adapter, communicates with an HTTP server by converting JSON through XHR.

3. The contents of this section are divided into two pieces. The first block covers the default behavior of an adapter, including JSON that requests records to access those URLs and what format they expect to return.

4. The second section covers how to override these default settings to customize something, such as which URLs data is requested and how JSON data is organized.

Second, URL conventions

1. This rest adapter uses the name of the model to determine what URL to send jsn.

For example, if you request a photo recordwith an ID:


default Ember.Route.extend ({  function(params) {    returnthis. Store.findrecord (' photo ', params.photo_id);  });

This rest adapter will automatically send a get request to /PHOTS/1.
2. You can take the action, which is mapped to the following URL in the rest adapter:

Action HTTP Verb URL
Find Record GET /posts/123
Find All GET /posts
Update PUT /posts/123
Create POST /posts
Delete DELETE /posts/123

Third, JSON conventions

Assume the following models:


Import ds from ' Ember-data 'default  ds. Model.extend ({  title:    ds.attr (),  comments:DS.hasMany (' comment '),  User:     Ds.belongsto (' user ')});


Import ds from ' Ember-data 'default  ds. Model.extend ({  body:DS.attr ()});

Ember data expects that a get request to /POSTS/1 will return JSON in the following format:

{  "post": {    "id": 1,    "title": "Rails is Omakase",    "comments": [" 1 "," 2 "],    " user ":" DHH "  },  " Comments ": [{    " id ":" 1 ",    "Body": "Rails is Unagi"  }, {    "id": "2",    "body": "Omakase O_o"  } ]}

Iv. Customizing the adapter

1. In order to customize this rest adapter, create a app/adapters/application.js file and export a DS. the subclass of the Restadapter. You can then override its properties and methods to customize how the records is retrieved and saved.


default DS. Restadapter.extend ({  ...});

2. Customizing a specific model

It is entirely possible that you need to define options for a model, rather than an application-scoped customization. In this case, you can create an adapter that is named with the specified model.


default DS. Restadapter.extend ({  ' api/v2 ',  ' https://api.example2.com '});


default DS. Restadapter.extend ({  ' api/v1 ',  ' https://api.example.com '});

This allows you to easily link to multiple API versions, while interacting with different domains on a per-model basis.

V. Customizing URLS

1. URL Prefix

If your JSON API is elsewhere instead of host root, you can set a prefix that can be added to all requests. For example, if you are using a JSON API, a special person who requests it may access /API/V1/PEOPLE/1. In this case, set the namespace property to api/v1.


default DS. Restadapter.extend ({  ' api/v1 '});

Requesting a person with ID 1 will now be accessed to /api/v1/people/1.

2. URL Host

If your JSON API is running on a different domain than the one that serves your Ember app, you can change the host to send HTTP requests.

Note In order to make it work, you need to use a browser that supports cors (cross-domain access), and your server needs to be configured to send the correct CORS header.

In order to change the host to which this request was sent, set the host property:


default DS. Restadapter.extend ({  ' https://api.example.com '});

Using ID 1 to request a person, you will now be navigated to https://api.example.com/people/1.

3. Custom HTTP Headers

Some APIs request HTTP headers, for example, to provide an API key. The arbitrary head can be set to Key/value on theRESTAdapterheaders属性中并且Ember Data将会随着每一次ajax请求发送它们。

For example:


default DS. Restadapter.extend ({  headers: {    ' api_key ': ' Secret KEY ',    ' another_header ': ' Some Header value '}  });

Requesting any resources will include the following HTTP headers:

Another_header:some HEADER Valueapi_key:secret KEY

7.9 Models--Connection to an HTTP Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.