EmberJS – Customizing Adapters

Adapter

Ember.js Adapter specifies how data is kept at the backend data store such as the URL format and the REST API headers. The default Adapter of Ember includes some built-in assumptions for REST API. These assumptions help to build a web application much easier and better.

The Adapter can be created by using the following command −

ember generate adapter adapter-name

When you run the above command, it will display the following lines −

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend ({
   // code goes here
});

Ember data has the following built-in adapters −

  • DS.Adapter − It is a basic adapter with no functionality in Ember.js.
  • DS.JSONAPIAdapter − It is a default adapter that interfaces with HTTP server and follows JSON API conventions by transferring JSON via XHR.
  • DS.RESTAdapter − It is used to communicate with HTTP server by using your store which transfers the JSON via XHR.

JSONAPIAdapter URL Conventions

The JSONAPIAdapter specifies URLs based on the model name.

For instance −

store.findRecord('mypost', 1).then(function(myfunc) {
});

The JSONAPIAdapter will send the GET request to /myposts/1, if you ask for MyPost by ID. The following actions can be used on records in JSONAPIAdapter −

S.No.ActionHTTP VerbURL
1FindGET/myposts/123
2Find AllGET/myposts
3UpdatePATCH/myposts/123
4CreatePOST/myposts
5DeleteDELETE/myposts/123

Endpoint Path Customization

The endpoint path can be customized by using the namespace property with specific url namespace.

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend ({
   namespace: 'api/1'
});

If you request for the myval model, then it will display the url as http://emberjs.com/api/1/myval/1.

Host Customization

You can specify the new domain by using the host property on the adapter −

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend ({
   host: 'https://api.mysite.com'
});

If you request for the myval model, then it will display the url as http://api.mysite.com/myval/1.

Path Customization

The JSONAPIAdapter generates the path name by pluralizing and dasherizing the model name. You can override the pathForType method, if this behavior does not confirm to backend.

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend ({
   pathForType: function(type) {
      return Ember.String.underscore(type);
   }
});

Headers Customization

The headers can be customized by providing the key/value pairs on the JSONAPIAdapter’s headers object and the Ember data will send key/value pair along with each ajax request.

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend ({
   headers: {
      'API_KEY': 'secret key',
      'ANOTHER_HEADER': 'header value'
   }
});

Authoring Adapters

The serializer can be specified by using the defaultSerializer adapter which is used only when specific serializer or serializer:application is not defined. It can be written as −

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend ({
   defaultSerializer: '-default
});

Previous Page:-Click Here

Leave a Reply