prettify

baseline

Javascript Collection Class

Javascript does not need a Template for Connection Classes.
One Class fits all!
Items.js:
function Items(oConstructor){
   
this.oPage=new Page();
   
this.items = [];
   
   
//this.sItem=sItem; //e.g.: "Person"
   
this.oConstructor=oConstructor;
};
Items.prototype.Unload = function() {
   
this.items.length = 0;
   
   
delete this.oConstructor;
   
delete this.oPage;
   
delete this.items;
};
Items.prototype.Add = function(oItem) {
   
//this.items[this.items.length] = oItem;
   
this.items.push(oItem);
};
Items.prototype.Clear=function(){
   
this.items=[];
   
//this.oPage.Clear();
};
Items.prototype.IsEqual = function(o) {
   
var oItem, nItm;
   
if (!this.oPage.IsEqual(o.oPage)) return false;
   
if (this.items.length !== o.items.length) return false;
   
for (nItm = 0; nItm <= this.items.length - 1; nItm++) {
       
if (!this.items[nItm].IsEqual(o.items[nItm])) return false;
   
}
   
return true;
}
Items.prototype.Copy = function(o) {
   
var oItem, nItm;
   
this.oPage.Copy(o.oPage);
   
this.items = [];
   
for (nItm = 0; nItm <= o.items.length - 1; nItm++) {
        oItem
= new this.oConstructor();
        oItem
.Copy(o.items[nItm]);
       
this.items[nItm] = oItem;
   
}
   
return this;
};
Items.CopyTo = function(o) {
    o
.Copy(this);
   
return this;
};
Items.prototype.Count = function() {
   
return this.items.length;
};
Items.prototype.toString = function() {
   
var sOut = "[";
    sOut
+= "[" + this.oPage.toString() + "]";

   
for (var nItm = 0; nItm <= this.items.length - 1; nItm++) {
        sOut
+= "[" + this.items[nItm].toString() + "]";
   
}
    sOut
+= "]";
   
//sOut+=this.items[this.items.length-1].toString();
   
return sOut;
};
Items.prototype.Parse=function(o){
   
this.oPage.Parse(o[0]);
   
var oItem;
   
var nItm;
   
this.items=[];
   
for(nItm=0; nItm <= o[1].length-1; nItm++){
       
//oItem = new Item();
       
//oItem=eval("new "+sItem+"()");
        oItem
= new this.oConstructor();
        oItem
.Parse(o[1][nItm]);
       
this.items[nItm]= oItem;
   
}
};
//--Client Server----------------------------------------------
Items.prototype.Load = function(fCb, sVals) {
   
this.LoadRe = function(oItems) {
       
return function(oResult) {
           
if (oResult && oResult.nError && oResult.nError == 1) {
                ins
.oUser.Login();
           
} else {
                oItems
.Parse(oResult);
                fCb
();
           
}
       
}
   
} (this);
   
var sData;
   
if (sVals == undefined) {
       
//o Jd.send(this.LoadRe, "sAction=get" + this.oConstructor.sId + "s&sVals=" + u$(this.oPage.toString()));
       
//ins.Post("getAll"+this.oConstructor.sId+"s" ,this.oPage.toString(), this.LoadRe);
        ins
.Post(this.oConstructor.sId + "s",(this.oPage.sAction==""?"getAll":this.oPage.sAction), this.oPage.toString(), this.LoadRe);
   
} else {
       
//o Jd.send(this.LoadRe, "sAction=get" + this.oConstructor.sId + "s&sVals=" + u$(sVals));
       
//ins.Post("getAll"+this.oConstructor.sId+"s" ,sVals, this.LoadRe);
        ins
.Post(this.oConstructor.sId + "s", "getAll", sVals, this.LoadRe);
   
}
};

1 comment: