Skip to content
Browse files

Merge pull request #4448 from bmac/improved-ajax-fallback

[BUGFIX beta] The adapter should call `ajax` instead of the new metho…
  • Loading branch information...
2 parents f4d7d5b + 7c94e33 commit e030fa69ee229b057050cbe323c6c3f024ca91de @bmac bmac committed on GitHub Jun 28, 2016
Showing with 101 additions and 17 deletions.
  1. +23 −2 addon/adapters/json-api.js
  2. +31 −11 addon/adapters/rest.js
  3. +3 −3 config/features.json
  4. +1 −1 package.json
  5. +43 −0 tests/integration/adapter/rest-adapter-test.js
View
25 addon/adapters/json-api.js
@@ -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
@@ -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');
@@ -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 = {};
@@ -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;
}
});
View
42 addon/adapters/rest.js
@@ -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 {
@@ -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'
@@ -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,
@@ -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'
@@ -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'
@@ -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'
@@ -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'
@@ -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'
@@ -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'
@@ -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'
@@ -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'
@@ -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;
}
});
View
6 config/features.json
@@ -1,9 +1,9 @@
{
- "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-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,
View
2 package.json
@@ -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",
View
43 tests/integration/adapter/rest-adapter-test.js
@@ -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 e030fa6

Please sign in to comment.
Something went wrong with that request. Please try again.