Skip to content

Commit

Permalink
[BUGFIX beta] The adapter should call ajax instead of the new metho…
Browse files Browse the repository at this point in the history
…ds if it has been customized.
  • Loading branch information
bmac committed Jun 28, 2016
1 parent 380299c commit 7c94e33
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 17 deletions.
25 changes: 23 additions & 2 deletions addon/adapters/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Ember from 'ember';
import RESTAdapter from "ember-data/adapters/rest";
import isEnabled from 'ember-data/-private/features';
import { deprecate } from 'ember-data/-private/debug';

/**
@class JSONAPIAdapter
Expand Down Expand Up @@ -99,7 +100,7 @@ var JSONAPIAdapter = RESTAdapter.extend({
@return {Promise} promise
*/
findMany(store, type, ids, snapshots) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
return this._super(...arguments);
} else {
var url = this.buildURL(type.modelName, ids, snapshots, 'findMany');
Expand All @@ -126,7 +127,7 @@ var JSONAPIAdapter = RESTAdapter.extend({
@return {Promise} promise
*/
updateRecord(store, type, snapshot) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
return this._super(...arguments);
} else {
var data = {};
Expand All @@ -139,6 +140,26 @@ var JSONAPIAdapter = RESTAdapter.extend({

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

_hasCustomizedAjax() {
if (this.ajax !== JSONAPIAdapter.prototype.ajax) {
deprecate('JSONAPIAdapter#ajax has been deprecated please use. `methodForRequest`, `urlForRequest`, `headersForRequest` or `dataForRequest` instead.', false, {
id: 'ds.json-api-adapter.ajax',
until: '3.0.0'
});
return true;
}

if (this.ajaxOptions !== JSONAPIAdapter.prototype.ajaxOptions) {
deprecate('JSONAPIAdapterr#ajaxOptions has been deprecated please use. `methodForRequest`, `urlForRequest`, `headersForRequest` or `dataForRequest` instead.', false, {
id: 'ds.json-api-adapter.ajax-options',
until: '3.0.0'
});
return true;
}

return false;
}
});

Expand Down
42 changes: 31 additions & 11 deletions addon/adapters/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from 'ember-data/adapters/errors';
import BuildURLMixin from "ember-data/-private/adapters/build-url-mixin";
import isEnabled from 'ember-data/-private/features';
import { runInDebug, warn } from 'ember-data/-private/debug';
import { runInDebug, warn, deprecate } from 'ember-data/-private/debug';
import parseResponseHeaders from 'ember-data/-private/utils/parse-response-headers';

const {
Expand Down Expand Up @@ -417,7 +417,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
findRecord(store, type, id, snapshot) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, id, snapshot,
requestType: 'findRecord'
Expand Down Expand Up @@ -449,7 +449,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
findAll(store, type, sinceToken, snapshotRecordArray) {
const query = this.buildQuery(snapshotRecordArray);

if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, sinceToken, query,
snapshots: snapshotRecordArray,
Expand Down Expand Up @@ -486,7 +486,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
query(store, type, query) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, query,
requestType: 'query'
Expand Down Expand Up @@ -522,7 +522,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
queryRecord(store, type, query) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, query,
requestType: 'queryRecord'
Expand Down Expand Up @@ -574,7 +574,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
findMany(store, type, ids, snapshots) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, ids, snapshots,
requestType: 'findMany'
Expand Down Expand Up @@ -623,7 +623,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
findHasMany(store, snapshot, url, relationship) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, snapshot, url, relationship,
requestType: 'findHasMany'
Expand Down Expand Up @@ -676,7 +676,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
findBelongsTo(store, snapshot, url, relationship) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, snapshot, url, relationship,
requestType: 'findBelongsTo'
Expand Down Expand Up @@ -709,7 +709,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
createRecord(store, type, snapshot) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, snapshot,
requestType: 'createRecord'
Expand Down Expand Up @@ -744,7 +744,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
updateRecord(store, type, snapshot) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, snapshot,
requestType: 'updateRecord'
Expand Down Expand Up @@ -776,7 +776,7 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
@return {Promise} promise
*/
deleteRecord(store, type, snapshot) {
if (isEnabled('ds-improved-ajax')) {
if (isEnabled('ds-improved-ajax') && !this._hasCustomizedAjax()) {
const request = this._requestFor({
store, type, snapshot,
requestType: 'deleteRecord'
Expand Down Expand Up @@ -1169,6 +1169,26 @@ var RESTAdapter = Adapter.extend(BuildURLMixin, {
}

return query;
},

_hasCustomizedAjax() {
if (this.ajax !== RESTAdapter.prototype.ajax) {
deprecate('RESTAdapter#ajax has been deprecated please use. `methodForRequest`, `urlForRequest`, `headersForRequest` or `dataForRequest` instead.', false, {
id: 'ds.rest-adapter.ajax',
until: '3.0.0'
});
return true;
}

if (this.ajaxOptions !== RESTAdapter.prototype.ajaxOptions) {
deprecate('RESTAdapter#ajaxOptions has been deprecated please use. `methodForRequest`, `urlForRequest`, `headersForRequest` or `dataForRequest` instead.', false, {
id: 'ds.rest-adapter.ajax-options',
until: '3.0.0'
});
return true;
}

return false;
}
});

Expand Down
6 changes: 3 additions & 3 deletions config/features.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"ds-boolean-transform-allow-null": null,
"ds-improved-ajax": null,
"ds-boolean-transform-allow-null": true,
"ds-improved-ajax": true,
"ds-pushpayload-return": null,
"ds-serialize-ids-and-types": true,
"ds-extended-errors": null,
"ds-links-in-record-array": null,
"ds-links-in-record-array": true,
"ds-overhaul-references": null,
"ds-payload-type-hooks": null,
"ds-check-should-serialize-relationships": null,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"license": "MIT",
"dependencies": {
"amd-name-resolver": "0.0.5",
"babel-plugin-feature-flags": "^0.2.0",
"babel-plugin-feature-flags": "^0.2.1",
"babel-plugin-filter-imports": "^0.2.0",
"broccoli-babel-transpiler": "^5.5.0",
"broccoli-file-creator": "^1.0.0",
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2593,3 +2593,46 @@ testInDebug("warns when an empty response is returned, though a valid stringifie

assert.expectWarning("The server returned an empty string for POST /posts, which cannot be parsed into a valid JSON. Return either null or {}.");
});


if (isEnabled('ds-improved-ajax')) {
testInDebug("The RESTAdapter should use `ajax` with a deprecation message when it is overridden by the user.", function(assert) {
assert.expect(2)

adapter.ajax = function(url, verb, hash) {
assert.ok(true, 'The ajax method should be called when it is overridden');
return { posts: { id: 1, name: "Rails is omakase" } };
};

assert.expectDeprecation(function() {
run(function() {
store.findRecord('post', 1);
});
}, /RESTAdapter#ajax has been deprecated/)
});


testInDebug("The RESTAdapter should use `ajaxOptions` with a deprecation message when it is overridden by the user.", function(assert) {
assert.expect(2)

adapter._ajaxRequest = function(hash) {
var jqXHR = {
status: 200,
getAllResponseHeaders() { return ''; }
};
hash.success({ posts: { id: 1, name: "Rails is omakase" } }, 'OK', jqXHR);
}

var oldAjaxOptions = adapter.ajaxOptions;
adapter.ajaxOptions = function() {
assert.ok(true, 'The ajaxOptions method should be called when it is overridden');
return oldAjaxOptions.apply(this, arguments);
};

assert.expectDeprecation(function() {
run(function() {
store.findRecord('post', 1);
});
}, /RESTAdapter#ajaxOptions has been deprecated/)
});
}

0 comments on commit 7c94e33

Please sign in to comment.