Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE ds-improved-ajax] Finer control over customizing a request #3099

Merged
merged 1 commit into from
Mar 21, 2016
Merged

[FEATURE ds-improved-ajax] Finer control over customizing a request #3099

merged 1 commit into from
Mar 21, 2016

Commits on Mar 18, 2016

  1. [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);
      }
    });
    ```
    pangratz committed Mar 18, 2016
    Configuration menu
    Copy the full SHA
    41f2089 View commit details
    Browse the repository at this point in the history