v-kitty
Version:
hello kitty
63 lines (55 loc) • 2 kB
JavaScript
(function (modules) {
var installedModules = {};
var xmlrequest = function(options){
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (4 == xmlhttp.readyState) {
if (200 == xmlhttp.status) {
var data = xmlhttp.responseText;
options.success(data);
}else{
options.error && options.error();
}
}
};
xmlhttp.open(options.type || 'POST', options.url , options.async===false?false:true);
if(!options.withoutCookie){
xmlhttp.withCredentials = true;
}
if(options){
xmlhttp.setRequestHeader('Accept', 'application/json, text/plain, */*');
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
for(var key in options.headers){
xmlhttp.setRequestHeader(key, options.headers[key]);
}
}
xmlhttp.send(options.data);
};
function _kitty_require(moduleId) {
if(installedModules[moduleId]){
return installedModules[moduleId].exports;
}
var module = installedModules[moduleId] = {
exports:{},
id:moduleId
};
if(/^http[s]:\/\//.test(moduleId)){
xmlrequest({
type:"get",
url:moduleId,
async:false,
success:function(res){
var fn = new Function('module','exports','_kitty_require',res);
fn.call(module.exports,module,_kitty_require)
},
error:function(){
console.error(url+' request fail');
}
})
}else{
modules[moduleId].call(module.exports,module,_kitty_require);
}
return module.exports;
}
return _kitty_require('{{key}}');
})({module_funs});