[FEATURE ds-improved-ajax] Finer control over customizing a request - #3099
Conversation
There was a problem hiding this comment.
I didn't want to update all assertions below, hence this weird setup; I think this can be refactored/simplified more once this is accepted ...
There was a problem hiding this comment.
Not relevant anymore, since everything is behind a feature flag...
|
Awesome thanks @pangratz |
|
Just a heads up: I am rebasing this PR onto latest |
|
💯 this is so awesome! I will be able to kill all the hacks I have in |
|
Is the thought to make 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. |
|
@workmanw I haven't added documentation for the methods yet, but I think it would be favorable to make those methods public. Currently |
|
I rebased onto latest For easier review of the commits which add the calls in the adapter methods, the whitespace should be ignored by appending |
|
I once again rebased onto latest |
| * @param {Object} params | ||
| * @return {Object} request object | ||
| */ | ||
| requestFor(params) { |
There was a problem hiding this comment.
Lets rename this to _requestFor.
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);
}
});
```
|
Looks good. |
[FEATURE ds-improved-ajax] Finer control over customizing a request
Though
ajax()(andajaxOptions()) ofDS.RESTAdapterare marked asprivate, 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 tocustomize the properties of a request:
methodForRequestto get the HTTP verburlForRequestto get the URLheadersForRequestto get the headersdataForRequestto get the data (query params or request body)The
paramshash passed to those methods has all the properties withwhich the corresponding
find,createRecord,findQuery, ... callhave been invoked: store, type, snapshot(s), id(s) and query. The
requestTypeproperty indicates which method is requested; the possiblevalues are:
createRecordupdateRecorddeleteRecordqueryqueryRecordfindRecordfindAllfindManyfindHasManyfindBelongsToPerforming the actual AJAX request is handled by the
makeRequestmethod, which is similar to the existing
ajaxmethod: it makes therequest using
jQuery.ajaxand attaches success and failure handlers.Say your API handles creation of resources via PUT, this can now be
customized as follows:
Credit for the initial idea goes to @igorT in #3047 (comment).