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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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