if (!this.HTTPServiceUtils) { HTTPServiceUtils = { }; } (function () { var XMLHttpReq ={ _objPool: [],//xmlhttp建立连接池 _getInstance:function (){ for (var i = 0; i < this._objPool.length; i ++){ if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4){ return this._objPool[i]; } } // IE5中不支持push方法 this._objPool[this._objPool.length] = this._createObj(); return this._objPool[this._objPool.length - 1]; }, _createObj: function (){ if (window.XMLHttpRequest){ var objXMLHttp = new XMLHttpRequest(); }else{ var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP']; for(var n=0; n<MSXML.length; n++){ try{ var objXMLHttp = new ActiveXObject(MSXML[n]); break; }catch(e){} } } // mozilla某些版本没有readyState属性 if (objXMLHttp.readyState == null){ objXMLHttp.readyState = 0; objXMLHttp.addEventListener("load", function (){ objXMLHttp.readyState = 4; if(typeof objXMLHttp.onreadystatechange == "function"){objXMLHttp.onreadystatechange();} }, false); } return objXMLHttp; }, // 发送请求(方法[post,get], 地址, 数据, 回调函数,数据模板) sendReq: function (method, url, data, callback,dataset){ var objXMLHttp = this._getInstance(); with(objXMLHttp){ try{ open(method, url, true); // 设定请求编码方式 setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); send(data); onreadystatechange = function (){ if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304)){ callback(objXMLHttp,dataset); } } }catch(e){alert(e);} } } }; var DynamicFields;//MAP格式 DynamicFields.keys()所有的键 DynamicFields.values()所有的值 var Datasetname; var Dataset; var _xml; //设置map字段 function setMap(_dynamicfields){ var map = new Map();//创建map对象 var fields = new Array(); fields = fields.str2arr(_dynamicfields); for(var i in fields) { if(!isNaN(i)){ var xx = new Array(); xx = fields[i].split(":"); map.put(xx[0],xx[1]); } } DynamicFields = map; } //数组对象的扩展增加一个字串转数组的方法 Array.prototype.str2arr=function(string){ var s; if(string.indexOf(";")!=-1) s=string.split(";"); if(string.indexOf("^")!=-1) s=string.split("^"); arr=this; for(i=0;i<s.length;i++){ arr = arr.concat(s[i]); } return arr; } //清除数组内容 Array.prototype.clear=function(){ this.splice(0,this.length); } // 回调函数jsonResponse function jsonResponse(originalRequest,_dataset) { var fields = new Array(); fields = _dataset._fields.keys(); var name = _dataset._name; var myobj = eval("(" + originalRequest.responseText + ")"); _xml = JsonToXml(myobj,name,fields) InitialDataset(_xml,_dataset); } //json 到 xml 的转换 function JsonToXml(_jsonobj,_datasetname,_fields){ var xml = "<xml id=\"__"+_datasetname+"\" style=\"display: none\">"; xml = xml +"<records possibleCount=\"1\" pageCount=\"1\" loadedPages=\"1\">"; var recNum=0; var itemCount = _jsonobj.itemCount; var fieldsCount = _fields.length; while(itemCount>0) { xml=xml+"<record id=\""+(recNum+1)+"\" pageIndex=\"1\" state=\"none\"><new>"; xml=xml+"s";//选择框占位符 true 表示勾选框勾住 for(var i=0;i<fieldsCount;i++){ xml=xml+"^"+_jsonobj.list[recNum][_fields[i]]; } xml= xml+"</new></record>"; recNum++; itemCount--; } xml = xml +"</records>"; xml = xml +"</xml>"; return xml; } //初始化数据集 function InitialDataset(_xml,_dataset) { var XMLDoc; _dataset.clearData(); XMLDoc = new ActiveXObject("Msxml2.DOMDocument"); XMLDoc.async = false; if (XMLDoc.loadXML(_xml)) { $cq=XMLDoc.getElementsByTagName("xml/records")[0]; _dataset.$8p($cq,true,null,null); return _dataset; } else { alert("初始化数据集错误!"); } } if(typeof HTTPServiceUtils.httpGet !== 'function'){ HTTPServiceUtils.httpGet = function(url,dataset){ XMLHttpReq.sendReq("get",url,null,jsonResponse,dataset); } } if(typeof HTTPServiceUtils.httpPost !== 'function'){ HTTPServiceUtils.httpPost = function(url,parameters,dataset){ XMLHttpReq.sendReq("post",url,parameters,jsonResponse,dataset); } } //初始化表格 if(typeof HTTPServiceUtils.initDataTable !== 'function'){ HTTPServiceUtils.initDataTable = function(_datatable,_dataset){ _datatable.addColumn("select"); for(var i=0;i<_dataset._fields.size();i++){ var _f = _datatable.addColumn(_dataset._fields.element(i).key); _f.setField(_dataset._fields.element(i).key) _f.setLabel(_dataset._fields.element(i).value) }// DynamicFields.keys()所有的键 DynamicFields.values()所有的值 } } //通过模板取得一个数据集实例 if(typeof HTTPServiceUtils.initDataset !== 'function'){ HTTPServiceUtils.initDataset = function(_dynamicfields,_datasetname,_keyname){ Datasetname = _datasetname; Dataset = BX.create("Dataset",null,_datasetname,""); setMap(_dynamicfields); Dataset._name = Datasetname; Dataset._fields = DynamicFields; Dataset._keyname = _keyname; Dataset.addField("select","string"); for(var i=0;i<DynamicFields.size();i++){ var _f = Dataset.addField(DynamicFields.element(i).key,"string"); _f.setLabel(DynamicFields.element(i).value); if(DynamicFields.elements[i].key == "key") _f.setToolTip(DynamicFields.element(i).value); } return Dataset; } } })();