Skip to content

Commit 17017af

Browse files
committed
Replacing jQuery AJAX with XHR
Removed old jQuery AHAX based _request & _raw_request functions New single function using raw XMLHttpRequest call to handle both Works in latest Chrome, Firefox & IE Means you no longer need remote jQuery dependency at all (Saves extra ~32k and using remote resource)
1 parent 88cc388 commit 17017af

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

github.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Github.js 0.6.1
1+
// Github.js 0.6.2
22
// (c) 2012 Michael Aufreiter, Development Seed
33
// Github.js is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -16,35 +16,22 @@
1616
function headers() {
1717
var headers = {}
1818
if (options.auth === 'oauth' && !options.token) return { Accept: 'application/vnd.github.raw' };
19-
if (options.auth === 'basic' && (!options.username || !options.password)) return { Accept: 'application/vnd.github.raw' };
19+
if (options.auth === 'basic' && (!options.username || !options.password)) return { Accept: 'application/vnd.github.raw' };
2020
return options.auth == 'oauth'
21-
? { Authorization: 'token '+ options.token, Accept: 'application/vnd.github.raw' }
21+
? { Authorization: 'token '+ options.token, Accept: 'application/vnd.github.raw' }
2222
: { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password), Accept: 'application/vnd.github.raw' }
2323
}
2424

25-
function _request(method, path, data, cb) {
26-
$.ajax({
27-
type: method,
28-
url: API_URL + path,
29-
data: JSON.stringify(data),
30-
dataType: 'json',
31-
contentType: 'application/x-www-form-urlencoded',
32-
success: function(res) { cb(null, res); },
33-
error: function(err) { cb(err); },
34-
headers : headers()
35-
});
36-
}
37-
38-
function _raw_request(method, path, data, cb) {
39-
$.ajax({
40-
type: method,
41-
url: API_URL + path,
42-
data: JSON.stringify(data),
43-
contentType: 'application/x-www-form-urlencoded',
44-
success: function(res) { cb(null, res); },
45-
error: function(err) { cb(err); },
46-
headers : headers()
47-
});
25+
function _request(method, path, data, cb, raw) {
26+
var xhr = new XMLHttpRequest();
27+
if (!raw) {xhr.dataType = "json"}
28+
xhr.open(method, API_URL + path);
29+
xhr.onreadystatechange = function () {
30+
if (this.readyState == 4) {
31+
this.status == 200 ? cb(null, JSON.parse(this.responseText)) : cb(this.status);
32+
}
33+
}
34+
data ? xhr.send(JSON.stringify(data)) : xhr.send();
4835
}
4936

5037
// User API
@@ -156,7 +143,7 @@
156143
// -------
157144

158145
this.getBlob = function(sha, cb) {
159-
_raw_request("GET", repoPath + "/git/blobs/" + sha, null, cb);
146+
_request("GET", repoPath + "/git/blobs/" + sha, null, cb, 'raw');
160147
};
161148

162149
// For a given file path, get the corresponding sha (blob for files, tree for dirs)

0 commit comments

Comments
 (0)