Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE ds-improved-ajax] Finer control over customizing a request
Though `ajax()` (and `ajaxOptions()`) of `DS.RESTAdapter` are marked as private, they have been overwritten in many applications, since there is currently no other way to customize the request. This feature adds new public methods to `DS.RESTAdapter`, which allow to customize the properties of a request: - `methodForRequest` to get the HTTP verb - `urlForRequest` to get the URL - `headersForRequest` to get the headers - `dataForRequest` to get the data (query params or request body) The `params` hash passed to those methods has all the properties with which the corresponding `find`, `createRecord`, `findQuery`, ... call have been invoked: store, type, snapshot(s), id(s) and query. The `requestType` property indicates which method is requested; the possible values are: - `createRecord` - `updateRecord` - `deleteRecord` - `query` - `queryRecord` - `findRecord` - `findAll` - `findMany` - `findHasMany` - `findBelongsTo` Performing the actual AJAX request is handled by the `_makeRequest` method, which is similar to the existing `ajax` method: it makes the request using `jQuery.ajax` and attaches success and failure handlers. --- Say your API handles creation of resources via PUT, this can now be customized as follows: ``` js // adapters/application.js import DS from 'ember-data'; export DS.RESTAdapter.extend({ methodForRequest: function(params) { if (params.requestType === 'createRecord') { return "PUT"; } return this._super(...arguments); } }); ```
- Loading branch information