Skip to content

Commit

Permalink
Resolve empty successful DELETES
Browse files Browse the repository at this point in the history
  • Loading branch information
bobjflong committed Oct 8, 2015
1 parent 55e0e9d commit 69d01f0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
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);
});
});

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);
});
});

0 comments on commit 69d01f0

Please sign in to comment.