Skip to content

Commit

Permalink
Adds corsWithCredentials option to DS.RESTAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Apr 8, 2013
1 parent fa946f9 commit 8608c61
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
19 changes: 18 additions & 1 deletion packages/ember-data/lib/adapters/rest_adapter.js
Expand Up @@ -59,6 +59,19 @@ var get = Ember.get, set = Ember.set, merge = Ember.merge;
}
}
```
## Customization
### Enable CORS with credentials
If your API is running on another domain and requires cookies to be included in every request
you have to enable this:
```js
DS.RESTAdapter.reopen({
corsWithCredentials: true
});
```
*/
DS.RESTAdapter = DS.Adapter.extend({
bulkCommit: false,
Expand Down Expand Up @@ -324,6 +337,11 @@ DS.RESTAdapter = DS.Adapter.extend({
if (hash.data && type !== 'GET') {
hash.data = JSON.stringify(hash.data);
}
if (this.corsWithCredentials) {
hash.xhrFields = {
withCredentials: true
};
}

jQuery.ajax(hash);
},
Expand Down Expand Up @@ -365,4 +383,3 @@ DS.RESTAdapter = DS.Adapter.extend({
return since ? query : null;
}
});

40 changes: 38 additions & 2 deletions packages/ember-data/tests/integration/rest_adapter_test.js
@@ -1,6 +1,42 @@
var get = Ember.get, set = Ember.set;
var store, adapter, Post, Comment;

module("REST Adapter") ;
module("REST Adapter", {
setup: function() {
adapter = DS.RESTAdapter.create();
store = DS.Store.create({
adapter: adapter
});

var attr = DS.attr;
Post = DS.Model.extend({
title: attr('string')
});
Post.toString = function() { return "Post"; };
},

teardown: function() {
Ember.run(function() {
store.destroy();
adapter.destroy();
});
}
});

test("if you specify corsWithCredentials then the withCredentials is passed to jquery", function() {
var hash;
window.jQuery._ajax = window.jQuery.ajax;
window.jQuery.ajax = function(ajaxHash) {
hash = ajaxHash;
};

set(adapter, 'corsWithCredentials', true);
var post = store.find(Post, 1);
deepEqual(hash.xhrFields, {withCredentials: true}, "xhrFields has withCredentials set to true.");
store.load(Post, { id: 1 });

window.jQuery.ajax = window.jQuery._ajax;
});

//test("changing A=>null=>A should clean up the record", function() {
//var store = DS.Store.create({
Expand Down Expand Up @@ -101,4 +137,4 @@ module("REST Adapter") ;
//deepEqual(john.get('kidneys').toArray(), [kidney1, kidney2], "john should have the first two kidneys again");
//deepEqual(jane.get('kidneys').toArray(), [kidney3], "jane should have the third kidney again");
//equal(kidney2.get('person'), john, "second kidney should be in john again");
//});
//});

0 comments on commit 8608c61

Please sign in to comment.