in the The adapter in the Ember application determines how the data is saved to the background, such as the URL format and the request header. the default adapter for Ember Data is the built-in REST API callback.
The default adapter is often extended in real-world use. Ember 's position is that you should add different features by extending the adapter, rather than adding identities. This makes the code easier to test, easier to understand, and reduces the code for adapters that might need to be extended.
If your backend is using the Ember The rules of the contract are then available using adapter adapters/application.js. The adapter application has a higher priority than the default adapter, but has a lower priority than the specified model adapter. The model adapter definition rule is:adapter-modelname.js. For example, the following code defines a model adapter adapter-post.
App/adapters/adapter-post.js import Ember from ' Ember '; export default DS. Jsonapiadapter.extend ({namespace: ' Api/v1 '});
the order of precedence for the adapter at this time is: Jsonapiadapter > Application > Default built-in adapter;
Ember built-in is equipped with the following types of:
Ds. Adapter This adapter is the most basic adapter and does not contain any functionality. If you need to create an adapter that is fundamentally different from the Ember adapter, you can start with this adapter.
Ds. Jsonapiadapter This adapter is the default adapter and is followed by the JSON API specification for interacting JSON data with the HTTP server through XHR .
Ds. Restadapter This adapter is the same as the second adapter, and is the default adapter before Ember Data2.0.
1, CustomJsonapiadapterAdapter
Jsonapiadapter adapters are typically used to extend non-standard background interfaces.
1,UrlSpecification
The Jsonapiadapter adapter is smart enough to automatically determine which URL link is the model. For example, if you need to get post by ID:
This.store.find (' Post ', 1). Then (function (POST) {//Processing post});
Jsonapiadapter will automatically send a GET request to /POST/1.
The following table is The mapping of Action, request, andURL .
Action |
HTTP |
Url |
find |
get |
/POSTS/1 |
find all |
get |
/posts |
update |
patch |
/POSTS/1 |
create |
post |
/posts |
delete |
delete |
/POSTS/1 |
For example , executing the Find () method in action, a GET request is sent , andJsonapiadapter automatically parses the shape such as /posts/1 URL.
2, diversified customization
in order to fit the model name of the duplicate name , it can be yo high Ember inflector binding alias.
Let Inflector = ember.inflector.inflector;inflector.irregular (' formula ', ' formulae '); Inflector.uncountable (' Advice ');
after this binding the purpose is to tell Jsonapiadapter. You can use /formulae/1 instead of/FORMULAS/1. But I haven't figured out what this setting means at the moment. What's the use? If the reader knows please advise.
3, breakpoint Path customization
Use the property namespace to set the prefix for the URL.
App/adapters/application.jsimport Ember from ' Ember '; export default DS. Jsonapiadapter.extend ({namespace: ' Api/1 '});
Request Person is automatically forwarded to /API/1/PEOPLE/1.
4, Custom hosts
By default, the adapter goes to the current domain name. If you want the URL to go to the new domain name you can use the property host setting.
App/adapters/application.jsimport Ember from ' Ember '; export default DS. Jsonapiadapter.extend ({host: ' http://api.example.com '});
Request Person is automatically forwarded to HTTP://API.EXAMPLE.COM/PEOPLE/1.
5, the custom path
By default Ember will attempt to generate a path based on the model name of the complex number, the model name separated by an underscore . If you need to change this default rule, you can use the property pathfortype setting.
App/adapters/application.jsimport Ember from ' Ember '; export default DS. Jsonapiadapter.extend ({pathfortype:function (type) {return Ember.String.underscore (type); }});
Modify the default build path rule to separate the glide lines. For example, request person will turn to/PERSON/1. Request User-profile will turn to/USER_PROFILE/1(default to USER-PROFILE/1).
6, custom Request headers
Some requests need to set up request header information, such as providing the key for the API. Header information can be set in the form of a key-value pair, andEmber data Adds this header information setting to each request.
App/adapters/application.jsimport Ember from ' Ember '; export default DS. Jsonapiadapter.extend ({headers: {' Api_key ': ' Secret KEY ', ' another_header ': ' Some HEADER value '}});
The more powerful part is that you can use the Calculation property in the header to dynamically set the request header information. The following code sets a session object to the adapter.
App/adapters/application.jsimport Ember from ' Ember '; export default DS. Jsonapiadapter.extend ({session:Ember.inject.service (' session '); headers:Ember.computed (' Session.authtoken ', function () {' Api_key ': This.get (' Session.authtoken '), ' Another_header ' : ' Some header value '});
You can also use the volatile () method to set the computed property to a non-cached property so that the value in the header is recalculated each time the request is sent.
App/adapters/application.jsimport Ember from ' Ember '; export default DS. Jsonapiadapter.extend ({//...}). Volatile ();});
For more information on adapter specific adapters, browse the following URLs:
· Ember Observer
· GitHub
· Bower
for the adapter main master Jsonapiadapter is enough, if you need a personalized URL or the requested domain name can be configured in the adaptation. In most cases, however, the default settings are used.
Ember.js Getting Started Guide--Custom adapters