Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds corsWithCredentials option to DS.RESTAdapter

  • Loading branch information...
commit 8608c6160ab09bd7bb8818684a94679250aadb2d 1 parent fa946f9
@johanneswuerbach johanneswuerbach authored
View
19 packages/ember-data/lib/adapters/rest_adapter.js
@@ -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,
@@ -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);
},
@@ -365,4 +383,3 @@ DS.RESTAdapter = DS.Adapter.extend({
return since ? query : null;
}
});
-
View
40 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({
@@ -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");
-//});
+//});
Please sign in to comment.
Something went wrong with that request. Please try again.