Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 213fb90

Browse files
author
Scott J. Miles
committed
core-ajax can pass body data directly to xhr for further processing; core-xhr makes decisions about the structuring request data based on method (see #3)
1 parent 78d1554 commit 213fb90

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

core-ajax.html

+2-10
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,8 @@
277277
*/
278278
go: function() {
279279
var args = this.xhrArgs || {};
280-
// TODO(sjmiles): we may want to default to POST if body is set
281-
if (this.bodyMethods[this.method.toUpperCase()]) {
282-
args.body = this.body || args.body;
283-
}
280+
// TODO(sjmiles): we may want XHR to default to POST if body is set
281+
args.body = this.body || args.body;
284282
args.params = this.params || args.params;
285283
if (args.params && typeof(args.params) == 'string') {
286284
args.params = JSON.parse(args.params);
@@ -301,12 +299,6 @@
301299
args.url = this.url;
302300
args.method = this.method;
303301
return args.url && this.xhr.request(args);
304-
},
305-
306-
bodyMethods: {
307-
POST: 1,
308-
PUT: 1,
309-
DELETE: 1
310302
}
311303

312304
});

core-xhr.html

+40-27
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,6 @@
3838

3939
Polymer('core-xhr', {
4040

41-
makeReadyStateHandler: function(xhr, callback) {
42-
xhr.onreadystatechange = function() {
43-
if (xhr.readyState == 4) {
44-
callback && callback.call(null, xhr.response, xhr);
45-
}
46-
};
47-
},
48-
49-
setRequestHeaders: function(xhr, headers) {
50-
if (headers) {
51-
for (var name in headers) {
52-
xhr.setRequestHeader(name, headers[name]);
53-
}
54-
}
55-
},
56-
57-
toQueryString: function(params) {
58-
var r = [];
59-
for (var n in params) {
60-
var v = params[n];
61-
n = encodeURIComponent(n);
62-
r.push(v == null ? n : (n + '=' + encodeURIComponent(v)));
63-
}
64-
return r.join('&');
65-
},
66-
6741
/**
6842
* Sends a HTTP request to the server and returns the XHR object.
6943
*
@@ -85,10 +59,13 @@
8559
var url = options.url;
8660
var method = options.method || 'GET';
8761
var async = !options.sync;
62+
//
8863
var params = this.toQueryString(options.params);
8964
if (params && method == 'GET') {
9065
url += (url.indexOf('?') > 0 ? '&' : '?') + params;
9166
}
67+
var xhrParams = this.isBodyMethod(method) ? (options.body || params) : null;
68+
//
9269
xhr.open(method, url, async);
9370
if (options.responseType) {
9471
xhr.responseType = options.responseType;
@@ -98,11 +75,47 @@
9875
}
9976
this.makeReadyStateHandler(xhr, options.callback);
10077
this.setRequestHeaders(xhr, options.headers);
101-
xhr.send(method == 'POST' ? (options.body || params) : null);
78+
xhr.send(xhrParams);
10279
if (!async) {
10380
xhr.onreadystatechange(xhr);
10481
}
10582
return xhr;
83+
},
84+
85+
toQueryString: function(params) {
86+
var r = [];
87+
for (var n in params) {
88+
var v = params[n];
89+
n = encodeURIComponent(n);
90+
r.push(v == null ? n : (n + '=' + encodeURIComponent(v)));
91+
}
92+
return r.join('&');
93+
},
94+
95+
isBodyMethod: function(method) {
96+
return this.bodyMethods[(method || '').toUpperCase()];
97+
},
98+
99+
bodyMethods: {
100+
POST: 1,
101+
PUT: 1,
102+
DELETE: 1
103+
},
104+
105+
makeReadyStateHandler: function(xhr, callback) {
106+
xhr.onreadystatechange = function() {
107+
if (xhr.readyState == 4) {
108+
callback && callback.call(null, xhr.response, xhr);
109+
}
110+
};
111+
},
112+
113+
setRequestHeaders: function(xhr, headers) {
114+
if (headers) {
115+
for (var name in headers) {
116+
xhr.setRequestHeader(name, headers[name]);
117+
}
118+
}
106119
}
107120

108121
});

0 commit comments

Comments
 (0)