Skip to content
This repository was archived by the owner on Apr 24, 2023. It is now read-only.
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
13 changes: 11 additions & 2 deletions packages/ember-model/lib/rest_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Ember.RESTAdapter = Ember.Adapter.extend({
url = this.buildURL(record.constructor, get(record, primaryKey)),
self = this;

return this.ajax(url, record.toJSON(), "DELETE").then(function(data) { // TODO: Some APIs may or may not return data
return this.ajax(url, record.toJSON(), "DELETE").then(function(data) {
self.didDeleteRecord(record, data);
});
},
Expand Down Expand Up @@ -124,6 +124,7 @@ Ember.RESTAdapter = Ember.Adapter.extend({
},

_ajax: function(url, params, method, settings) {
var self = this;
if (!settings) {
settings = this.ajaxSettings(url, method);
}
Expand All @@ -148,14 +149,22 @@ Ember.RESTAdapter = Ember.Adapter.extend({
jqXHR.then = null;
}

Ember.run(null, reject, jqXHR);
self._handleRejections(method, jqXHR, resolve, reject);
};


Ember.$.ajax(settings);
});
},

_handleRejections: function(method, jqXHR, resolve, reject) {
if (method === "DELETE" && jqXHR.status >= 200 && jqXHR.status < 300) {
Ember.run(null, resolve, null);
} else {
Ember.run(null, reject, jqXHR);
}
},

_loadRecordFromData: function(record, data) {
var rootKey = get(record.constructor, 'rootKey'),
primaryKey = get(record.constructor, 'primaryKey');
Expand Down
30 changes: 30 additions & 0 deletions packages/ember-model/tests/adapter/rest_adapter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,33 @@ test("buildURL() creates url from model's url, id, and url suffix", function() {
url = adapter.buildURL(RESTModel, "123");
equal(url, "/posts/123.json" );
});

test('_handleRejections() will resolve empty successful DELETEs', function() {
expect(1);
var resolve = function(data) {
ok(data === null, "resolved with null");
};
Ember.run(function() {
adapter._handleRejections("DELETE", {status: 200}, resolve, Ember.$.noop);
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth adding a test for the rejection case, ie different HTTP verb, status code other than 200.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

});

test('_handleRejections() will reject empty responses for other verbs', function() {
expect(1);
var reject = function(data) {
ok(data.status === 200, "rejected with 200 status");
};
Ember.run(function() {
adapter._handleRejections("PUT", {status: 200}, Ember.$.noop, reject);
});
});

test('_handleRejections() will reject DELETEs with unsuccessful status codes', function() {
expect(1);
var reject = function(data) {
ok(data.status === 403, "rejected with 403 status");
};
Ember.run(function() {
adapter._handleRejections("DELETE", {status: 403}, Ember.$.noop, reject);
});
});