-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
adapter.ajax = function(request) { | ||
passedUrl = request.url; | ||
passedVerb = request.method; | ||
passedHash = request.body ? { data: request.body } : undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not relevant anymore, since everything is behind a feature flag...
urlForRequest(params) { | ||
var { type, id, ids, snapshot, snapshots, requestType, query } = params; | ||
|
||
// type and id is not passed from updateRecord and deleteRecord, hence they |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/is/are/
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.RESTAdapter
are 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:
methodForRequest
to get the HTTP verburlForRequest
to get the URLheadersForRequest
to get the headersdataForRequest
to get the data (query params or request body)The
params
hash 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
requestType
property indicates which method is requested; the possiblevalues 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 therequest 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:
Credit for the initial idea goes to @igorT in #3047 (comment).