Skip to content

Commit 77dd300

Browse files
Merge pull request #13 from Xitstrategies/master
Implement ajaxSetup for reverse compatibility with $.ajaxSetup
2 parents 621a61e + 6c705ba commit 77dd300

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

can-ajax.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var Global = require("can-globals/global/global");
4-
var assign = require("can-assign");
4+
var canReflect = require("can-reflect");
55
var namespace = require("can-namespace");
66
var parseURI = require('can-parse-uri');
77
var param = require("can-param");
@@ -36,9 +36,31 @@ var param = require("can-param");
3636
* - __data__ `{Object}` The data of the request. If data needs to be urlencoded (e.g. for GET requests or for CORS) it is serialized with [can-param].
3737
* - __dataType__ `{String}` Type of data. _Default is `json`_.
3838
* - __crossDomain__ `{Boolean}` If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. Default: `false` for same-domain requests, `true` for cross-domain requests.
39+
* - __xhrFields__ `{Object}` Any fields to be set directly on the xhr request, [https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest] such as the withCredentials attribute that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.
3940
*
4041
* @return {Promise} A Promise that resolves to the data. The Promise instance is abortable and exposes an `abort` method. Invoking abort on the Promise instance indirectly rejects it.
4142
*
43+
*
44+
* @signature `ajaxSetup( ajaxOptions )`
45+
*
46+
* Is used to persist ajaxOptions across all ajax requests and they can be over-written in the ajaxOptions of the actual request.
47+
* [https://api.jquery.com/jquery.ajaxsetup/]
48+
*
49+
* ```
50+
* var ajax = require("can-ajax");
51+
*
52+
* ajax.ajaxSetup({xhrFields: {withCredentials: true}});
53+
*
54+
* ajax({
55+
* url: "http://query.yahooapis.com/v1/public/yql",
56+
* data: {
57+
* format: "json",
58+
* q: 'select * from geo.places where text="sunnyvale, ca"'
59+
* }
60+
* }).then(function(response){
61+
* console.log( response.query.count ); // => 2
62+
* });
63+
* ```
4264
*/
4365

4466
// from https://gist.github.com/mythz/1334560
@@ -52,6 +74,8 @@ var xhrs = [
5274
// used to check for Cross Domain requests
5375
var originUrl = parseURI(Global().location.href);
5476

77+
var globalSettings = {};
78+
5579
var makeXhr = function () {
5680
if (_xhrf != null) {
5781
return _xhrf();
@@ -92,7 +116,7 @@ var _xhrResp = function (xhr, options) {
92116
}
93117
};
94118

95-
module.exports = namespace.ajax = function (o) {
119+
function ajax(o) {
96120
var xhr = makeXhr(), timer, n = 0;
97121
var deferred = {};
98122
var promise = new Promise(function(resolve,reject){
@@ -105,13 +129,15 @@ module.exports = namespace.ajax = function (o) {
105129
xhr.abort();
106130
};
107131

108-
o = assign({
109-
userAgent: "XMLHttpRequest",
110-
lang: "en",
111-
type: "GET",
112-
data: null,
113-
dataType: "json"
114-
}, o);
132+
o = [{
133+
userAgent: "XMLHttpRequest",
134+
lang: "en",
135+
type: "GET",
136+
data: null,
137+
dataType: "json"
138+
}, globalSettings, o].reduce(function(a,b,i) {
139+
return canReflect.assignDeep(a,b);
140+
});
115141

116142
// Set the default contentType
117143
if(!o.contentType) {
@@ -179,7 +205,7 @@ module.exports = namespace.ajax = function (o) {
179205

180206
// For CORS to send a "simple" request (to avoid a preflight check), the following methods are allowed: GET/POST/HEAD,
181207
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests
182-
208+
183209
var isSimpleCors = o.crossDomain && ['GET', 'POST', 'HEAD'].indexOf(type) !== -1;
184210

185211
if (isPost) {
@@ -200,6 +226,17 @@ module.exports = namespace.ajax = function (o) {
200226
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
201227
}
202228

229+
if (o.xhrFields) {
230+
for (var f in o.xhrFields) {
231+
xhr[f] = o.xhrFields[f];
232+
}
233+
}
234+
203235
xhr.send(data);
204236
return promise;
237+
}
238+
239+
module.exports = namespace.ajax = ajax;
240+
module.exports.ajaxSetup = function (o) {
241+
globalSettings = o || {};
205242
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
]
4848
},
4949
"dependencies": {
50-
"can-assign": "^1.0.0",
5150
"can-globals": "^0.2.2",
5251
"can-namespace": "^1.0.0",
5352
"can-param": "^1.0.1",
54-
"can-parse-uri": "^1.0.0"
53+
"can-parse-uri": "^1.0.0",
54+
"can-reflect": "^1.4.5"
5555
},
5656
"devDependencies": {
5757
"can-make-map": "^1.0.0",

test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!DOCTYPE html>
12
<title>can-ajax</title>
23
<script src="node_modules/steal/steal.js" main="can-ajax/can-ajax-test"></script>
34
<div id="qunit-fixture"></div>

0 commit comments

Comments
 (0)