-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
120 lines (94 loc) · 3.01 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// A collection of utility methods to help build the Simple Feed Viewer
//
(function (window) {
// Creates a wrapper function in the global scope that is deleted after it is invoked
// - This function should remain private to util.js
function createGlobalCallback(fn) {
var fnName = 'fn' + new Date().valueOf();
window[fnName] = function() {
fn.apply(this, arguments);
try {
delete window[fnName];
} catch (e) {
window[fnName] = null;
}
};
return fnName;
}
// Makes a JSONP request to an API endpoint
// - This function should remain private to util.js
function makeJSONPRequest(options) {
var head = document.getElementsByTagName('head')[0];
var scriptEl = document.createElement('script');
var src = options.url;
options.data || (options.data = {});
options.data.callback = createGlobalCallback(function (data) {
if (data.status === 'fail') {
options.error(data);
} else if (data.status === 'ok') {
options.success(data);
}
head.removeChild(scriptEl);
});
scriptEl.onerror = function (event) {
options.error('{ status: "fail", mesg: "JSONP request failed to load" }');
head.removeChild(scriptEl);
};
scriptEl.id = options.data.callback;
scriptEl.async = true;
scriptEl.src = src + '&' + util.param(options.data);
head.appendChild(scriptEl);
}
// Public namespace for utility methods
var util = {
// Helper noop
noop: function() {},
// Converts a JavaScript object to a querystring
param: function (data) {
var out = [];
var n;
if (typeof data !== 'object') {
throw new Error('util.param: data arugment is not an object');
}
for (n in data) {
if (data.hasOwnProperty(n)) {
out.push(n + '=' + data[n]);
}
}
return out.join('&');
},
// Sends an asynchronous HTTP request.
ajax: function(options) {
var req;
// Set some defaults for async requests
options || (options = {});
options.method = options.method || 'GET';
options.success = options.success || util.noop;
options.error = options.error || util.noop;
// Our default dataType for this app is JSONP
options.dataType = options.dataType || 'jsonp';
if ( ! options.url) {
throw new Error('util.xhr: missing options.url');
}
// JSONP request
if (options.dataType === 'jsonp') {
makeJSONPRequest(options);
// GET method XHR
} else {
options.url = options.url + '&' + util.param(options.data);
req = new XMLHttpRequest();
req.open(options.method, options.url, true);
req.onload = function (event) {
var resp = JSON.parse(req.response);
options.success(resp);
};
req.onerror = function (event) {
var resp = req.response;
options.error(resp, event);
};
req.send();
}
}
};
window.util = util;
})(this);