Skip to content

Commit

Permalink
Merge pull request #3099 from pangratz/request_options
Browse files Browse the repository at this point in the history
[FEATURE ds-improved-ajax] Finer control over customizing a request
  • Loading branch information
bmac committed Mar 21, 2016
2 parents 307dd36 + 41f2089 commit e57f67e
Show file tree
Hide file tree
Showing 11 changed files with 673 additions and 128 deletions.
6 changes: 6 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ entry in `config/features.json`.
`store.findRecord()` and `store.findAll()` as described in [RFC
99](https://github.com/emberjs/rfcs/pull/99)

- `ds-improved-ajax`

This feature allows to customize how a request is formed by overwriting
`methodForRequest`, `urlForRequest`, `headersForRequest` and `bodyForRequest`
in the `DS.RESTAdapter`.

- `ds-references`

Adds references as described in [RFC 57](https://github.com/emberjs/rfcs/pull/57)
Expand Down
85 changes: 76 additions & 9 deletions addon/adapters/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

import Ember from 'ember';
import RESTAdapter from "ember-data/adapters/rest";
import isEnabled from 'ember-data/-private/features';

/**
@class JSONAPIAdapter
@constructor
@namespace DS
@extends DS.RESTAdapter
*/
export default RESTAdapter.extend({
var JSONAPIAdapter = RESTAdapter.extend({
defaultSerializer: '-json-api',

/**
Expand Down Expand Up @@ -98,8 +99,12 @@ export default RESTAdapter.extend({
@return {Promise} promise
*/
findMany(store, type, ids, snapshots) {
var url = this.buildURL(type.modelName, ids, snapshots, 'findMany');
return this.ajax(url, 'GET', { data: { filter: { id: ids.join(',') } } });
if (isEnabled('ds-improved-ajax')) {
return this._super(...arguments);
} else {
var url = this.buildURL(type.modelName, ids, snapshots, 'findMany');
return this.ajax(url, 'GET', { data: { filter: { id: ids.join(',') } } });
}
},

/**
Expand All @@ -121,14 +126,76 @@ export default RESTAdapter.extend({
@return {Promise} promise
*/
updateRecord(store, type, snapshot) {
var data = {};
var serializer = store.serializerFor(type.modelName);
if (isEnabled('ds-improved-ajax')) {
return this._super(...arguments);
} else {
var data = {};
var serializer = store.serializerFor(type.modelName);

serializer.serializeIntoHash(data, type, snapshot, { includeId: true });
serializer.serializeIntoHash(data, type, snapshot, { includeId: true });

var id = snapshot.id;
var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
var id = snapshot.id;
var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');

return this.ajax(url, 'PATCH', { data: data });
return this.ajax(url, 'PATCH', { data: data });
}
}
});

if (isEnabled('ds-improved-ajax')) {

JSONAPIAdapter.reopen({

methodForRequest(params) {
if (params.requestType === 'updateRecord') {
return 'PATCH';
}

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

dataForRequest(params) {
const { requestType, ids } = params;

if (requestType === 'findMany') {
return {
filter: { id: ids.join(',') }
};
}

if (requestType === 'updateRecord') {
const { store, type, snapshot } = params;
const data = {};
const serializer = store.serializerFor(type.modelName);

serializer.serializeIntoHash(data, type, snapshot, { includeId: true });

return data;
}

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

headersForRequest() {
const headers = this._super(...arguments) || {};

headers['Accept'] = 'application/vnd.api+json';

return headers;
},

_requestToJQueryAjaxHash() {
const hash = this._super(...arguments);

if (hash.contentType) {
hash.contentType = 'application/vnd.api+json';
}

return hash;
}

});

}

export default JSONAPIAdapter;
Loading

0 comments on commit e57f67e

Please sign in to comment.