49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
var FutamiCommon = function(vars) {
|
|
vars = vars || {};
|
|
|
|
var get = function(name, fallback) {
|
|
return vars[name] || fallback || null;
|
|
};
|
|
|
|
return {
|
|
get: get,
|
|
getJson: function(name, onload, onerror, noCache) {
|
|
if(typeof onload !== 'function')
|
|
throw 'onload must be specified';
|
|
|
|
var xhr = new XMLHttpRequest;
|
|
xhr.onload = function() {
|
|
onload(JSON.parse(xhr.responseText));
|
|
};
|
|
if(typeof onerror === 'function')
|
|
xhr.onerror = function() { onerror(); };
|
|
xhr.open('GET', get(name));
|
|
if(noCache)
|
|
xhr.setRequestHeader('Cache-Control', 'no-cache');
|
|
xhr.send();
|
|
},
|
|
};
|
|
};
|
|
|
|
FutamiCommon.load = function(callback, url) {
|
|
if(typeof callback !== 'function')
|
|
throw 'there must be a callback';
|
|
if(typeof url !== 'string' && 'FUTAMI_URL' in window)
|
|
url = window.FUTAMI_URL + '?t=' + Date.now().toString();
|
|
|
|
var xhr = new XMLHttpRequest;
|
|
xhr.onload = function() {
|
|
try {
|
|
callback(new FutamiCommon(JSON.parse(xhr.responseText)));
|
|
} catch(ex) {
|
|
callback(false);
|
|
}
|
|
};
|
|
xhr.onerror = function() {
|
|
callback(false);
|
|
};
|
|
xhr.open('GET', url);
|
|
xhr.setRequestHeader('Cache-Control', 'no-cache');
|
|
xhr.send();
|
|
};
|