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

Conversation

Projects
None yet
5 participants
@pangratz
Member

pangratz commented May 24, 2015

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:

// adapters/application.js
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  methodForRequest: function(params) {
    if (params.requestType === 'createRecord') {
      return "PUT";
    }

    return this._super(...arguments);
  }
});

Credit for the initial idea goes to @igorT in #3047 (comment).

@SeyZ

This comment has been minimized.

Show comment
Hide comment
@SeyZ

SeyZ Oct 21, 2015

Awesome thanks @pangratz

SeyZ commented Oct 21, 2015

Awesome thanks @pangratz

@pangratz

This comment has been minimized.

Show comment
Hide comment
@pangratz

pangratz Dec 21, 2015

Member

Just a heads up: I am rebasing this PR onto latest master and also try to put everything behind a feature flag...

Member

pangratz commented Dec 21, 2015

Just a heads up: I am rebasing this PR onto latest master and also try to put everything behind a feature flag...

@tchak

This comment has been minimized.

Show comment
Hide comment
@tchak

tchak Dec 22, 2015

Member

💯 this is so awesome! I will be able to kill all the hacks I have in ajaxOptions :)

Member

tchak commented Dec 22, 2015

💯 this is so awesome! I will be able to kill all the hacks I have in ajaxOptions :)

@workmanw

This comment has been minimized.

Show comment
Hide comment
@workmanw

workmanw Dec 22, 2015

Contributor

Is the thought to make headersForRequest and bodyForRequest public? I'm currently using ajaxOptions and had no idea it was technically private. Several SO answers and blog posts mention using ajaxOptions.

Either way, there should definitely be a public way to do this that isn't declarative. Things like CSRFs and sessions can be rotating constantly.

Contributor

workmanw commented Dec 22, 2015

Is the thought to make headersForRequest and bodyForRequest public? I'm currently using ajaxOptions and had no idea it was technically private. Several SO answers and blog posts mention using ajaxOptions.

Either way, there should definitely be a public way to do this that isn't declarative. Things like CSRFs and sessions can be rotating constantly.

@pangratz

This comment has been minimized.

Show comment
Hide comment
@pangratz

pangratz Dec 22, 2015

Member

@workmanw I haven't added documentation for the methods yet, but I think it would be favorable to make those methods public. Currently ajax and ajaxOptions are private but they are overwritten many times in different apps since there is currently no other way to customize the request...

Member

pangratz commented Dec 22, 2015

@workmanw I haven't added documentation for the methods yet, but I think it would be favorable to make those methods public. Currently ajax and ajaxOptions are private but they are overwritten many times in different apps since there is currently no other way to customize the request...

@pangratz pangratz changed the title from Split construction of AJAX options into separate methods to [FEATURE ds-improved-ajax] Finer control over customizing a request Dec 23, 2015

@pangratz

This comment has been minimized.

Show comment
Hide comment
@pangratz

pangratz Dec 23, 2015

Member

I rebased onto latest master branch and put everything behind a feature flag ds-improved-ajax. Also, rudimentary documentation has been added for the new public methods.

For easier review of the commits which add the calls in the adapter methods, the whitespace should be ignored by appending ?w=1 to the GitHub commit URL:

Member

pangratz commented Dec 23, 2015

I rebased onto latest master branch and put everything behind a feature flag ds-improved-ajax. Also, rudimentary documentation has been added for the new public methods.

For easier review of the commits which add the calls in the adapter methods, the whitespace should be ignored by appending ?w=1 to the GitHub commit URL:

@pangratz

This comment has been minimized.

Show comment
Hide comment
@pangratz

pangratz Mar 16, 2016

Member

I once again rebased onto latest master and squashed all commits into 1. This is ready to go @bmac.

Member

pangratz commented Mar 16, 2016

I once again rebased onto latest master and squashed all commits into 1. This is ready to go @bmac.

Show outdated Hide outdated addon/adapters/rest.js
Show outdated Hide outdated addon/adapters/rest.js
Show outdated Hide outdated addon/adapters/rest.js
Show outdated Hide outdated addon/adapters/rest.js
[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);
  }
});
```
@bmac

This comment has been minimized.

Show comment
Hide comment
@bmac

bmac Mar 21, 2016

Member

Looks good.

Member

bmac commented Mar 21, 2016

Looks good.

bmac added a commit that referenced this pull request Mar 21, 2016

Merge pull request #3099 from pangratz/request_options
[FEATURE ds-improved-ajax] Finer control over customizing a request

@bmac bmac merged commit e57f67e into emberjs:master Mar 21, 2016

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@pangratz pangratz deleted the pangratz:request_options branch Mar 21, 2016

@amiel amiel referenced this pull request Sep 27, 2016

Closed

query keys in query string #19

@sandstrom sandstrom referenced this pull request Jan 18, 2018

Open

[QUEST] Making jQuery Optional #5320

2 of 6 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment