利用js的闭包原理做对象封装及调用方法
程序员文章站
2022-03-21 15:33:19
创建一个js文件,名为testclosure.js:
(function () {
function a()
{
alert('i am a...
创建一个js文件,名为testclosure.js:
(function () { function a() { alert('i am a'); } outfunc = function () { a(); } })();
这里不论写多少个function,a b c d ...外面都调用不到,包括这里面var定义的变量也都调用不到,那么你在里面尽情的写,就不用担心这些函数名变量名跟外界冲突;
只需要暴露一个outfunc这个函数供外界调用。这个函数呢没有用var定义,就变成一个全局变量,外界就可以调用的到,利用这一点,让这个函数变成匿名函数和外界沟通的桥梁。
再利用js面向对象的方法,就可以封装出非常好用的组件。
示例一:不需要继承的js组件
(function() { var arrauthitem = new array(); var isinited = false; var synctableobj=findobj("sync-table-id",document); var newtr=null; var checkbox=null; var authtable = null; var selfpicurl=null; var selfitem=null; var isauthitemenabled=false; var isselfitemenabled=false; function getauthshopcurrent() { return $("#"+globalsyncvars.servercomclientid.authshoplistid).val(); } function getsyncfieldcurrent() { return $("#"+globalsyncvars.servercomclientid.syncfieldlistid).val(); } function settitle() { $("#sync-table-title-id").html("从“"+getauthshopcurrent()+"”同步"); } function getnumiidfrom(numiidto) { var currowdata = jquery("#listitemdefine").jqgrid('getrowdata', numiidto); return currowdata.numiidfrom; } function insertrows() { deleteall(); for(var i=0;i<arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""].length;i++) { newtr=synctableobj.insertrow(synctableobj.rows.length); // 0 checkbox checkbox=newtr.insertcell(0); // 1 auth table authtable=newtr.insertcell(1); // 2 self picurl selfpicurl=newtr.insertcell(2); // 3 self item selfitem=newtr.insertcell(3); isselfitemenabled=setterselfitem(i); isauthitemenabled=setterauthitem(i); if(isauthitemenabled&&isselfitemenabled) setterenablestatus(true,i); else setterenablestatus(false,i); } } function setterauthitem(i) { var isenabled=false; if (!stringtoboolean(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["hassameitem"])) { authtable.innerhtml='<table class="inner-table-class inner-table-from-class"><tr><td class="inner-table-td-radio-class"></td><td class="inner-table-td-class main-img-class"></td><td class="inner-table-td-class" style="padding-top:21px;padding-bottom:21px;">此宝贝没有对应的授权宝贝</td><td class="inner-table-td-class sync-img-class"><span class="icon-no-class"/></td></tr></table>'; return isenabled; } var arr = arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["listitemfrom"]; var strhead = '<table class="inner-table-class inner-table-from-class">'; var strtr = ""; var isbinded = false; // 如果已经绑定了,标记一下,是否已经授权过也标记 var numiid = getnumiidfrom(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["numiidto"]); if(numiid!="none") { for(var j=0;j<arr.length;j++) { if(arr[j]["numiid"]==numiid) { isbinded=true; break; } } } for(var p=0;p<arr.length;p++) { var issharedfrom = stringtoboolean(arr[p]["isfromitemhassharefrom"]); var issharedto = stringtoboolean(arr[p]["isfromitemhasshareto"]); var picurl='<a href="http://item.taobao.com/item.htm?id=' + arr[p][" rel="external nofollow" numiid"] + '" target="_blank"><img class="icon" style="padding:0px;width:60px" src="' + arr[p]["picurl"] + '_sum.jpg" title="' + arr[p]["title"] + '" /></a>'; if(getenabledstatus(isbinded,!issharedto,isselfitemenabled)) isenabled = true; strtr+='<tr><td class="inner-table-td-radio-class">'+getradiocode(isbinded,!issharedto,isselfitemenabled,i,arr.length)+'</td><td class="inner-table-td-class main-img-class">'+picurl+'</td><td class="inner-table-td-class">'+getauthitemcode(arr[p],issharedfrom,issharedto,i)+'</td><td class="inner-table-td-class sync-img-class">'+getenabledcode(isbinded,!issharedto,isselfitemenabled,arr[p],numiid)+'</td></tr>'; } authtable.innerhtml = strhead+strtr+'</table>'; return isenabled; } // 返回auth宝贝详情html代码 function getauthitemcode(obj,issharedfrom,issharedto,i) { var labelstr = ""; if(issharedfrom) labelstr = '<span style="color:red;">【源】</span>'; if(issharedto) labelstr = '<span style="color:red;">【受】</span>'; return '<span style="line-height:20px;" columnname="numiid">'+labelstr+'id:' + obj["numiid"] + '</span>' + '<span style="padding-left:14px;" columnname="outerid">商家编码:' + obj["outerid"] + '</span>' + '<span style="padding-left:14px;color:'+getpricecolor(obj["price"],i)+'" columnname="price">价格:' + obj["price"] + '</span>' + '<br><span style="line-height:20px;" columnname="title">' + obj["title"] + '</span>'; } // 返回self宝贝详情html代码 function getselfitemcode(obj) { var labelstr = ""; if(stringtoboolean(obj["istoitemhassharefrom"])) labelstr = '<span style="color:red;">【源】</span>'; if(stringtoboolean(obj["istoitemhasshareto"])) labelstr = '<span style="color:red;">【受】</span>'; return '<span class="sync-table-td-class"><span style="line-height:20px;" columnname="numiid">'+labelstr+'id:' + obj["numiidto"] + '</span>' + '<span style="padding-left:14px;" columnname="outerid">商家编码:' + obj["outeridto"] + '</span>' + '<span style="padding-left:14px;color:black;" columnname="price">价格:' + obj["priceto"] + '</span></span>' + '<br><span class="sync-table-td-class" style="line-height:20px;" columnname="title">' + obj["titleto"] + '</span>'; } // 返回绑定关系图标的html代码 function getenabledcode(isbinded,isauthenabled,isselfenabled,obj,numiid) { if(isbinded) { // 如果是绑定状态,还要看当前item是否为绑定的item // 在这种情况下,判断是否可绑定,不需要用到selfitem if(obj["numiid"]==numiid) return '<span class="icon-bind-class"/>'; else { if(stringtoboolean(obj["isfromitemhasshareto"])) return '<span class="icon-no-class"/>'; else return '<span class="icon-ok-class"/>'; } }else { if(isauthenabled&&isselfenabled) return '<span class="icon-ok-class"/>'; else return '<span class="icon-no-class"/>'; } } // 是否可以绑定 function getenabledstatus(isbinded,isauthenabled,isselfenabled) { if(isbinded) return false; if(isauthenabled&&isselfenabled) { return true; } else { return false; } } // 返回radio的html代码 /** * isbinded 是否已经绑定 * isauthenabled auth是否可同步 * isselfenabled self是否可同步 * 当前rows的index * 当前auth-list的长度(如果是一对一,当然不需要radio) */ function getradiocode(isbinded,isauthenabled,isselfenabled,i,length) { if(isbinded) return ""; if(length==1) return ""; if(isauthenabled==false||isselfenabled==false) return ""; return '<input type="radio" name="radio'+i+'">'; } function setterselfitem(i) { var isenabled=true; var picurl='<a href="http://item.taobao.com/item.htm?id=' + arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+" rel="external nofollow" "][i]["numiidto"] + '" target="_blank"><img class="icon" style="padding-left:11px;width:60px" src="' + arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["picurlto"] + '_sum.jpg" title="' + arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["titleto"] + '" /></a>'; selfpicurl.innerhtml = picurl; selfitem.innerhtml = getselfitemcode(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]); if(stringtoboolean(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["istoitemhassharefrom"])) isenabled = false; if(stringtoboolean(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["istoitemhasshareto"])) isenabled = false; return isenabled; } function getpricecolor(price,i) { if(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["priceto"]==undefined) return "black"; if(price==undefined) return "black"; return (price==arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["priceto"])?"black":"red"; } function setterenablestatus(isenabled,i) { if(isenabled) checkbox.innerhtml='<input class="enablecheckbox" type="checkbox" checked="checked" index="'+i+'"/>'; else checkbox.innerhtml='<input type="checkbox" disabled="true"/>'; } function stringtoboolean(str) { if (str==undefined) return false; switch(str.tolowercase()) { case "true": case "yes": case "1": return true; case "false": case "no": case "0": case null: return false; default: return boolean(str); } } function deleteall() { for(var i=synctableobj.rows.length-1;i>2;i--) { synctableobj.deleterow(i); } } function selectall(isselect) { if(isselect) { $(".enablecheckbox").prop("checked",true); }else { $(".enablecheckbox").prop("checked",false); } } function findobj(theobj, thedoc) { var p, i, foundobj; if(!thedoc) thedoc = document; if((p = theobj.indexof("?")) > 0 && parent.frames.length) { thedoc = parent.frames[theobj.substring(p+1)].document; theobj = theobj.substring(0,p); } if(!(foundobj = thedoc[theobj]) && thedoc.all) foundobj = thedoc.all[theobj]; for (i=0; !foundobj && i < thedoc.forms.length; i++) foundobj = thedoc.forms[i][theobj]; for(i=0; !foundobj && thedoc.layers && i < thedoc.layers.length; i++) foundobj = findobj(theobj,thedoc.layers[i].document); if(!foundobj && document.getelementbyid) foundobj = document.getelementbyid(theobj); return foundobj; } function refrash() { deleteall(); if(!isinited) return; settitle(); //现在js还没有数据的,去取值 if(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""]==null) { var urledit="./x.aspx?method=getauthitem&rmd="+math.random(); $.post(urledit,$("#"+globalsyncvars.servercomclientid.form1).serialize()) .done(function(myjsonresult) { var data = $.evaljson(myjsonresult); ajaxresponseresult(data); if (data.issuccess) { arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""]=data.rows; insertrows(); $("#sync-table-id").css("display","inline-table"); } }) .fail(function(data) { ajaxresponseresult(data); }) .always(function(myjsonresult) { }); }else { insertrows(); } } function checkisenabledbind(i) { if(stringtoboolean(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["isfromitemhasshareto"])) return false; if(stringtoboolean(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["istoitemhassharefrom"])) return false; if(stringtoboolean(arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][i]["istoitemhasshareto"])) return false; return true; } function setsyncvaluetocom() { var numiidsself=""; var numiidsauth=""; var isbind=""; var isradiounchecked = false; $(".enablecheckbox:checked").each(function() { if(checkisenabledbind($(this).attr("index"))) { var checkboxindex = $(this).attr("index"); var radioindex = 0; isradiounchecked = false; // 通过[0]判断radio是否存在 if($('input:radio[name=radio'+checkboxindex+']')[0]) { var ischeckedlabel = false; // 检查每个radio选项是否被选择 $('input:radio[name=radio'+checkboxindex+']').each(function() { if($('input:radio[name=radio'+checkboxindex+']')[radioindex].checked) { ischeckedlabel = true; // 这里是跳出each循环(里层) return false; } radioindex++; }); if(!ischeckedlabel) isradiounchecked = true; // 如果存在checkbox有选择,但是radio没选择,应该提醒 // 然后就退出了,没有后续的提交 if(isradiounchecked) { checkboxindex++; alert("第"+checkboxindex+"个宝贝有多个对应的授权宝贝,应该仔细查看并选择合适的宝贝同步!"); // 这里跳出循环(外层) return false; } } var arrauth = arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][checkboxindex]["listitemfrom"] if(numiidsself=="") { numiidsself+=arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][checkboxindex]["numiidto"]; }else { numiidsself+=","+arrauthitem[getauthshopcurrent()+getsyncfieldcurrent()+""][checkboxindex]["numiidto"]; } if(numiidsauth=="") { numiidsauth+=arrauth[radioindex]["numiid"]; }else { numiidsauth+=","+arrauth[radioindex]["numiid"]; } } }); isbind=$("#isbind:checked").val()=="on"?"true":"false"; $("#"+globalsyncvars.servercomclientid.hfnumiidsself).val(numiidsself); $("#"+globalsyncvars.servercomclientid.hfnumiidsauth).val(numiidsauth); $("#"+globalsyncvars.servercomclientid.hfisbind).val(isbind); if(isradiounchecked) return -1; if(numiidsself=="") return 0; else return 1; } inittable=function() { refrash(); isinited=true; } onselectallcheckbox=function() { if($("#selectallcheckbox:checked").val()=="on") { selectall(true); }else { selectall(false); } } onpreview=function() { refrash(); } onaldsitemsync=function(sender,jqgridobjrefresh) { var callbackcode = setsyncvaluetocom(); if(callbackcode == 0) { alert("没有可以同步设置的宝贝!"); return; }else if(callbackcode == -1) { // 当存在没有选择的radio时 return; } var urledit="./x.aspx?method=savesyncitem&rmd="+math.random(); $.post(urledit,$("#"+globalsyncvars.servercomclientid.form1).serialize()) .done(function(myjsonresult) { var data = $.evaljson(myjsonresult); ajaxresponseresult(data); if (data.issuccess) { showprompt(data.promptmsg); if(sender) { $(sender).dialog("close"); $(sender).dialog("destroy"); } if(jqgridobjrefresh) { $(jqgridobjrefresh).trigger("reloadgrid"); } } }) .fail(function(data) { ajaxresponseresult(data); }) .always(function(myjsonresult) { }); } })();
示例二:有继承关系的js组件
(function(){ //____________________________批量修改的基类_________________________________ function batchmodify(){} //选择了什么id batchmodify.prototype.numiids=null; batchmodify.prototype.addis=null; batchmodify.prototype.oneiid=null; batchmodify.prototype.skuids=null; batchmodify.prototype.selecteditemdata=null; batchmodify.prototype.maxheightval=$(window).height()*0.9; // 完全克隆一个数组 batchmodify.prototype.clonearr=function(arr) { var arrnew = []; for(var i=0;i<arr.length;i++) { arrnew.push(arr[i]); } return arrnew; } // 通过jqgrid('getgridparam','selarrrow');取值是不可靠的,经常会变动 // 在设置jqgrid('setselection',bindedarr[i]);之后取出来的值经常会变化,要么长度变化,要么顺序变化 batchmodify.prototype.cancelbindeditem=function() { var tempnumiids=jquery("#listitemdefine").jqgrid('getgridparam','selarrrow'); var tempnumiidsclone = this.clonearr(tempnumiids); var isalert=false; for(var i=0;i<bindedarr.length;i++) { for(var j=0;j<tempnumiidsclone.length;j++) { if(tempnumiidsclone[j]==bindedarr[i]) { isalert=true; jquery("#listitemdefine").jqgrid('setselection',bindedarr[i]); break; } } } if(isalert) alert("已经绑定了数据来源的宝贝不能修改宝贝设置,如果需要修改请先点击宝贝最右栏的解绑按钮解绑!"); } batchmodify.prototype.setnumiids=function() { this.numiids=jquery("#listitemdefine").jqgrid('getgridparam','selarrrow'); }; //拼装addi的json串并赋值给addi batchmodify.prototype.setaddis=function() { var _addis="{rows:{"; for(var i=0;i<this.numiids.length;i++) { _addis+='"'+this.numiids[i]+'":"'; var tempval=$("#additextid"+this.numiids[i]).val(); var val=tempval.replace(/\"/ig,"\'"); _addis+=val; _addis+='"' if(i!=this.numiids.length-1) { _addis+=','; } } _addis+="}}"; this.addis=_addis; }; //(单个)修改页面传进来的一个id batchmodify.prototype.setoneiid=function() { this.oneiid=$("#"+itemeditvars.itemeditclientid.hfnumiid).val(); }; //sku的所有已选id(一个字符串) batchmodify.prototype.setskuids=function() { this.skuids=jquery("#listskudefine").jqgrid('getgridparam','selarrrow'); }; //设置numiids的值到服务器组件 batchmodify.prototype.setnumiidstocom=function() { $("#"+globalvars.servercomclientid.hfitemdefineidsselect).val(this.numiids); }; //设置addis的值到服务器组件 batchmodify.prototype.setaddistocom=function() { $("#"+globalvars.servercomclientid.hfaddistr).val(this.addis); }; //检查是否有勾选宝贝 batchmodify.prototype.checkisselected=function() { if (this.numiids == "") { alert("请选中要批量修改的行!"); return false; } return true; }; //批量生成描述 batchmodify.prototype.batchgeneratedesc=function(alertresult) { this.setnumiidstocom(); var urledit = "x.aspx?method=batchgeneratedesc&rdm=" + math.random(); if (alertresult) urledit += "&generatebymanual=true"; this.postdata(urledit,alertresult); }; //post数据 batchmodify.prototype.postdata=function(urledit,alertresult) { $.post( urledit, $("#"+globalvars.servercomclientid.formaldsitemdefine1).serialize()) .done(function(myjsonresult) { var data = $.evaljson(myjsonresult); ajaxresponseresult(data); if (data.issuccess) { if (!data.rows) showprompt("宝贝描述生成修改成功!"); } if(alertresult && data.errmsg && $.trim(data.errmsg) != "") alert("提醒:\n\n" + data.errmsg); }) .fail(function(data) { ajaxresponseresult(data); }) .always(function(myjsonresult) { }); }; batchmodify.prototype.removedialog=function() { try{ $("#desctempdiv").remove(); } catch(e){} } function myinherits(child,parent) { function f(){} f.prototype=parent.prototype; child.prototype=new f(); child.prototype.constructor=child; } function createobject(proto) { function f(){} f.prototype=proto; return new f(); } //_________________________批量修改(附言,确认收货后发消息,好评后发消息)类_________________________________ function batchmsgmodify(title,tableid,paperid,fieldname,editurl,containerdivstr) { batchmodify.call(); this.title=title; this.tableid=tableid; this.paperid=paperid; this.fieldname=fieldname; this.editurl=editurl; this.containerdivstr=containerdivstr; } myinherits(batchmsgmodify,batchmodify); //弹窗 batchmsgmodify.prototype.popupdialog=function() { $(this.containerdivstr).dialog({ title: this.title, closetext: '关闭', width: 980, maxheight: $(window).height() * 0.98, minheight: $(window).height() * 0.7, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 确定: function() { currentinstance.confirmcallback(this); }, 关闭: function() { $(this).dialog("close"); } } }); }; //创建表格 batchmsgmodify.prototype.createtable=function() { $("#"+this.tableid).jqgrid({ viewrecords: true, // show the current page, data rang and total records on the toolbar datatype: 'local', rownum:100, rowlist:[100], height: "auto", pager: "#"+this.paperid, colnames:['主图','宝贝','numiid','price','outerid',this.title.indexof("批量修改")>=0?this.title.substring(4):this.title], colmodel:[ {name:'picurl', index: 'picurl', width: 60, align: "center", sortable: false, formatter: imgformatter, unformat:imgunformat}, {name:'title',index:'outerid', width:430, sortable: true, formatter: itemdescformatter, unformat:itemdescunformat}, {name:'numiid',index:'numiid', width:100, hidden:true, key:true}, {name:'price',index:'price', width:100, hidden:true}, {name: 'outerid', index: 'outerid', width: 100, hidden: true }, {name: this.fieldname, index: this.fieldname, width: 440, sortable: true, hidden: false, formatter:this.formatter} ] }); }; //把表格填充进数据 batchmsgmodify.prototype.inittable=function() { var gridarraydata=[]; for(var i=0;i<this.numiids.length;i++) { var currowdata=jquery("#listitemdefine").jqgrid('getrowdata',this.numiids[i]); var obj=new object(); obj["picurl"]=currowdata.picurl; obj["title"]=currowdata.title; obj["numiid"]=currowdata.numiid; obj["price"]=currowdata.price; obj["outerid"]=currowdata.outerid; obj[this.fieldname]=currowdata[this.fieldname]; gridarraydata.push(obj); } $("#"+this.tableid).jqgrid('setgridparam',{data:gridarraydata}); $("#"+this.tableid).trigger('reloadgrid'); }; //格式化字段 batchmsgmodify.prototype.formatter=function(cellvalue,options,rowdata) { return "<textarea id='additextid"+rowdata.numiid+"' style='width:98%;' rows='4'>"+rowdata[currentinstance.fieldname]+"</textarea>"; }; //重写post函数 batchmsgmodify.prototype.postdata=function(urledit,sender) { $.post( urledit, $("#"+globalvars.servercomclientid.formaldsitemdefine1).serialize()) .done(function(myjsonresult) { var data = $.evaljson(myjsonresult); ajaxresponseresult(data); if (data.issuccess) { if (!data.rows) showprompt(currentinstance.title+"成功"+currentinstance.numiids.length+"个!"); $("#listitemdefine").trigger('reloadgrid'); settimeout(function () { $(sender).dialog("close"); }, 0); settimeout(function () { $(sender).dialog("destroy"); }, 1000); } if(data.errmsg && $.trim(data.errmsg) != "") alert(currentinstance.title+"出错:\n\n" + data.errmsg); }) .fail(function(data) { ajaxresponseresult(data); }) .always(function(myjsonresult) { }); }; //确认按钮的回调函数 batchmsgmodify.prototype.confirmcallback=function(sender) { this.setaddis(); this.setnumiidstocom(); this.setaddistocom(); this.postdata(this.editurl+"&rdm=" + math.random(),sender); }; //实例 var batchaddi=null; var batchmsgtradesuccess=null; var batchmsgratedgood=null; var currentinstance=null;//指向当前对象,当this指向失效的时候可以使用这个 openbatchmsgdialogtype={batchaddi:0,batchmsgtradesuccess:1,batchmsgratedgood:2}; openbatchmsgdialog=function(typecode) { if(typecode==openbatchmsgdialogtype.batchaddi) { if (batchaddi==null) batchaddi=new batchmsgmodify("批量修改附言","tableeditcontent","tablepaper","additional","x.aspx?method=updatemsgadditional","<div id='editbatchdiv'><table id='tableeditcontent'></table><div id='tablepaper'></div></div>"); currentinstance=batchaddi; batchaddi.cancelbindeditem(); batchaddi.setnumiids(); if(!batchaddi.checkisselected()) return; batchaddi.popupdialog(); batchaddi.createtable(); batchaddi.inittable(); }else if (typecode==openbatchmsgdialogtype.batchmsgtradesuccess) { if (batchmsgtradesuccess==null) batchmsgtradesuccess=new batchmsgmodify("批量修改确认收货后发消息","tableeditmsgtradesuccesscontent","tablemsgtradesuccesspaper","msgtradesuccess","x.aspx?method=updatemsgtradesuccess","<div id='editbatchdiv'><table id='tableeditmsgtradesuccesscontent'></table><div id='tablemsgtradesuccesspaper'></div></div>"); currentinstance=batchmsgtradesuccess; batchmsgtradesuccess.cancelbindeditem(); batchmsgtradesuccess.setnumiids(); if(!batchmsgtradesuccess.checkisselected()) return; batchmsgtradesuccess.popupdialog(); batchmsgtradesuccess.createtable(); batchmsgtradesuccess.inittable(); }else if (typecode==openbatchmsgdialogtype.batchmsgratedgood) { if (batchmsgratedgood==null) batchmsgratedgood=new batchmsgmodify("批量修改好评后发消息","tableeditmsgratedgoodcontent","tablemsgratedgoodpaper","msgratedgood","x.aspx?method=updatemsgratedgood","<div id='editbatchdiv'><table id='tableeditmsgratedgoodcontent'></table><div id='tablemsgratedgoodpaper'></div></div>"); currentinstance=batchmsgratedgood; batchmsgratedgood.cancelbindeditem(); batchmsgratedgood.setnumiids(); if(!batchmsgratedgood.checkisselected()) return; batchmsgratedgood.popupdialog(); batchmsgratedgood.createtable(); batchmsgratedgood.inittable(); } } //_______________________________批量修改宝贝类(弹出单独页面,只要弹窗即可)_________________________________ function batchitemmodify(){} myinherits(batchitemmodify,batchmodify); //重写 batchitemmodify.prototype.popupdialog=function() { var urledit = "./x.aspx?numiids="+this.numiids+"&rdm="+math.random(); $.get(urledit, {}, function (data, textstatus, object) { var str = "<div id='editbatchdiv'>" + object.responsetext + "</div>"; $(str).dialog({ dialogclass: "untitle", closetext: '关闭', width: 800, height: 400, maxheight: $(window).height() * 0.9, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 确定: function() { submitaldsitemeditbatch(this, $("#listitemdefine")); //在x.aspx定义 currentinstance.batchgeneratedesc(); }, 关闭: function() { $( this ).dialog( "close" ); } } }); }); } //实例 var batchitem=null; openbatchitemdialog=function() { if (batchitem==null) batchitem=new batchitemmodify(); currentinstance=batchitem; batchitem.cancelbindeditem(); batchitem.setnumiids(); if(!batchitem.checkisselected()) return; batchitem.popupdialog(); } //_______________________________批量生成宝贝描述__________________________________________ function batchdescmodify(){} myinherits(batchdescmodify,batchmodify); batchdescmodify.prototype.generating=function() { if (!confirm("提醒。\n\n是否继续?")) return; this.batchgeneratedesc(true); }; //实例 var batchdesc=null; onbatchdesc=function() { if(batchdesc==null) batchdesc=new batchdescmodify(); currentinstance=batchdesc; batchdesc.setnumiids(); if(!batchdesc.checkisselected()) return; batchdesc.generating(); } //_____________________________________修改模板______________________________________________ ondescmoulddialog=function() { var urledit = "./x.aspx?rdm="+math.random(); $.get(urledit,{},function(data,textstatus,object){ var str="<div id='desctempdiv'>"+object.responsetext+"</div>"; $(str).dialog({ title:'修改描述模板', closetext:'关闭', width:850, maxheight:$(window).height() * 0.98, modal:true, show:{effect:"fade",duration:300}, hide:{effect:"fade",duration:300}, buttons:{ 确定:function() { ontempparachangehandler(this); }, 关闭:function() { $(this).dialog("close"); } } }); }); } //___________________________________批量修改sku_______________________________________ function batchskumodify(){} myinherits(batchskumodify,batchmodify); batchskumodify.prototype.popupdialog=function() { var urledit = "./x.aspx?numiids="+this.oneiid+"&skuids="+this.skuids+"&isskueditbatch=true&rdm="+math.random(); $.get(urledit, {}, function (data, textstatus, object) { var str = "<div id='desctempdiv'>" + object.responsetext + "</div>"; $(str).dialog({ dialogclass: "untitle", closetext: '关闭', width: 800, height: 400, maxheight: $(window).height() * 0.9, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 确定: function() { submitaldsitemeditbatch(this, $("#listskudefine")); //在x.aspx定义 }, 关闭: function() { $( this ).dialog( "close" ); } } }); }); }; batchskumodify.prototype.checkisselected=function() { if (this.skuids == "") { alert("请选中要批量修改的sku!"); return false; } return true; }; //实例 var batchsku=null; openbatchskudialog=function() { if(batchsku==null) batchsku=new batchskumodify(); currentinstance=batchsku; batchsku.setoneiid(); batchsku.setskuids(); batchsku.removedialog(); if(!batchsku.checkisselected()) return; batchsku.popupdialog(); } //_______________________________批量修改sku附言__________________________________________________ function batchskumsgmodify(title,tableid,paperid,fieldname,editurl,containerdivstr) { batchmodify.call(); this.title=title; this.tableid=tableid; this.paperid=paperid; this.fieldname=fieldname; this.editurl=editurl; this.containerdivstr=containerdivstr; } myinherits(batchskumsgmodify,batchmodify); batchskumsgmodify.prototype.popupdialog=function() { $(this.containerdivstr).dialog({ title: this.title, closetext: '关闭', width: 980, maxheight: this.maxheightval, // height: this.maxheightval>750 ? 750 : this.maxheightval, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 确定: function() { currentinstance.confirmcallback(this); }, 关闭: function() { $(this).dialog("close"); } } }); }; //重写-创建表格 batchskumsgmodify.prototype.createtable=function() { $("#"+this.tableid).jqgrid({ viewrecords: true, // show the current page, data rang and total records on the toolbar datatype: 'local', rownum:100, rowlist:[100], height: "auto", pager: "#"+this.paperid, colnames:['skuid','属性名',this.title.indexof("批量修改")>=0?this.title.substring(4):this.title], colmodel:[ {name:'skuid',index:'skuid', width:400, hidden:true, key:true}, {name:'propertiesname',index:'propertiesname', width:400,sortable:false, editable: false, hiden:false}, {name: this.fieldname, index: this.fieldname, width: 440, sortable: true, hidden: false, formatter:this.formatter} ] }); }; //把表格填充进数据 batchskumsgmodify.prototype.inittable=function() { var gridarraydata=[]; for(var i=0;i<this.skuids.length;i++) { var currowdata=jquery("#listskudefine").jqgrid('getrowdata',this.skuids[i]); var obj=new object(); obj["skuid"]=currowdata.skuid; obj["propertiesname"]=currowdata.propertiesname; obj[this.fieldname]=currowdata[this.fieldname]; gridarraydata.push(obj); } $("#"+this.tableid).jqgrid('setgridparam',{data:gridarraydata}); $("#"+this.tableid).trigger('reloadgrid'); }; //重写-格式化字段 batchskumsgmodify.prototype.formatter=function(cellvalue,options,rowdata) { return "<textarea id='additextid"+rowdata.skuid+"' style='width:98%;' rows='4'>"+rowdata[currentinstance.fieldname]+"</textarea>"; }; //重写post函数 batchskumsgmodify.prototype.postdata=function(urledit,sender) { $.post( urledit, $("#"+itemeditvars.itemeditclientid.formaldsitemedit1).serialize()) .done(function(myjsonresult) { var data = $.evaljson(myjsonresult); ajaxresponseresult(data); if (data.issuccess) { if (!data.rows) showprompt(currentinstance.title+"成功"+currentinstance.skuids.length+"个!"); $("#listskudefine").trigger('reloadgrid'); settimeout(function () { $(sender).dialog("close"); }, 0); settimeout(function () { $(sender).dialog("destroy"); }, 1000); } if(data.errmsg && $.trim(data.errmsg) != "") alert(currentinstance.title+"出错:\n\n" + data.errmsg); }) .fail(function(data) { ajaxresponseresult(data); }) .always(function(myjsonresult) { }); }; //重写-拼装json batchskumsgmodify.prototype.setaddis=function() { var _addis="{rows:{"; for(var i=0;i<this.skuids.length;i++) { _addis+='"'+this.skuids[i]+'":"'; var tempval=$("#additextid"+this.skuids[i]).val(); var val=tempval.replace(/\"/ig,"\'"); _addis+=val; _addis+='"' if(i!=this.skuids.length-1) { _addis+=','; } } _addis+="}}"; this.addis=_addis; }; //重写-设置numiids到组件 batchskumsgmodify.prototype.setnumiidstocom=function() { $("#"+itemeditvars.itemeditclientid.hfnumiid).val(this.oneiid); }; //新增-设置skuids到组件 batchskumsgmodify.prototype.setskuidstocom=function() { $("#"+itemeditvars.itemeditclientid.hfskuids).val(this.skuids); }; //重写 batchskumsgmodify.prototype.setaddistocom=function() { $("#"+itemeditvars.itemeditclientid.hfskuaddistr).val(this.addis); }; //确认按钮的回调函数 batchskumsgmodify.prototype.confirmcallback=function(sender) { this.setaddis(); this.setnumiidstocom(); this.setskuidstocom(); this.setaddistocom(); currentinstance.postdata(this.editurl+"&rdm=" + math.random(),sender); }; // batchskumsgmodify.prototype.checkisselected=function() { if (this.skuids == "") { alert("请选中要批量修改附言的sku!"); return false; } return true; }; //实例 var batchskuaddi=null; openbatchskumsgdialogtype={batchskuaddi:0}; openbatchskumsgdialog=function(typecode) { if (typecode==openbatchskumsgdialogtype.batchskuaddi) { if (batchskuaddi==null) batchskuaddi=new batchskumsgmodify("批量修改sku附言","tableeditcontent","tablepaper","additional","x.aspx?method=updatemsgadditional","<div id='desctempdiv'><table id='tableeditcontent'></table><div id='tablepaper'></div></div>"); currentinstance=batchskuaddi; batchskuaddi.removedialog(); batchskuaddi.setoneiid(); batchskuaddi.setskuids(); if(!batchskuaddi.checkisselected()) return; batchskuaddi.popupdialog(); batchskuaddi.createtable(); batchskuaddi.inittable(); } } //_______________________________生成描述_________________________________________________ var descbatchmodify=null; batchgeneratedesc=function(descnumids,alertresult) { if(descbatchmodify==null) descbatchmodify=new batchmodify(); descbatchmodify.numiids=descnumids; descbatchmodify.setnumiidstocom(); descbatchmodify.batchgeneratedesc(alertresult); } //_______________________________同步类(弹出单独页面,只要弹窗即可)_________________________________ function syncmodify(){} myinherits(syncmodify,batchmodify); //重写 syncmodify.prototype.popupdialog=function() { var urledit="./x.aspx?rdm="+math.random(); $.post(urledit,{numiids:this.numiids.tostring()},function(data,textstatus,object){ var str = "<div id='desctempdiv'>" + object.responsetext + "</div>"; $(str).dialog({ title:"同步设置", closetext: '关闭', width: 1150, height: 800, maxheight: $(window).height() * 0.9, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 同步: function() { onaldsitemsync(this, $("#listitemdefine")); //在x.aspx定义 }, 取消: function() { $( this ).dialog( "close" ); } } }); }); } var syncmodify=null; opensyncdialog=function() { if(syncmodify==null) syncmodify=new syncmodify(); currentinstance=syncmodify; syncmodify.setnumiids(); if(!syncmodify.checkisselected()) return; syncmodify.removedialog(); syncmodify.popupdialog(); } //____________________________同步sku设置(弹出单独页面,只需弹窗)___________________________________ function syncskumodify(){} myinherits(syncskumodify,batchmodify); syncskumodify.prototype.popupdialog=function() { var urledit="./x.aspx?rdm="+math.random(); $.post(urledit,{numiid:currentinstance.oneiid.tostring(),skuids:currentinstance.skuids.tostring()},function(data,textstatus,object){ var str = "<div id='desctempdiv'>" + object.responsetext + "</div>"; $(str).dialog({ title:"同步sku设置", closetext: '关闭', width: 1150, height: 800, maxheight: $(window).height() * 0.9, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 同步: function() { onaldsskusync(this, $("#listskudefine")); //在x.aspx定义 }, 取消: function() { $( this ).dialog( "close" ); } } }); }); } syncskumodify.prototype.checkisselected=function() { if (this.skuids == "") { alert("请选中要同步设置的sku!"); return false; } return true; }; var syncskumodify=null; openskusyncdialog=function() { if($("#"+itemeditvars.itemeditclientid.hfnumiidfrom).val()=="none") { alert("没有找到已经绑定的宝贝!要同步sku设置,需要先绑定对应的宝贝,才能同步宝贝下面的sku!"); return; } if(syncskumodify==null) syncskumodify=new syncskumodify(); currentinstance=syncskumodify; syncskumodify.setoneiid(); syncskumodify.setskuids(); if (!syncskumodify.checkisselected()) return; syncskumodify.removedialog(); syncskumodify.popupdialog(); } //__________________________item批量(及单独)解除绑定_________________________________ function batchdisbindingitem(){} myinherits(batchdisbindingitem,batchmodify); // 对非绑定item取消勾选,是为了下一步批量解除绑定 batchdisbindingitem.prototype.cancelfreeitem=function() { var isalert = false; var isbinded = false; var tempnumiids = jquery("#listitemdefine").jqgrid('getgridparam', 'selarrrow'); var tempnumiidsclone = this.clonearr(tempnumiids); for (var i = 0; i < tempnumiidsclone.length; i++) { var numiidtmp = tempnumiidsclone[i]; var currowdata = jquery("#listitemdefine").jqgrid('getrowdata', numiidtmp); if (currowdata.numiidfrom == "none") { isalert = true; jquery("#listitemdefine").jqgrid('setselection', numiidtmp); } } if (isalert) { alert("未绑定的宝贝不能作解绑的动作!"); // return true 表示已经alert了 return true; } return false; } batchdisbindingitem.prototype.popupdialog=function() { var showstr='<div id="desctempdiv"><span style="color:red;">注意:您正在批量解除绑定,如果您解除了绑定,今后数据源宝贝设置有更新您的宝贝设置将不会同步更新!</span></div>'; $(showstr).dialog({ title: "解除绑定", closetext: '关闭', width: 600, height:300, maxheight: $(window).height() * 0.98, minheight: $(window).height() * 0.7, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 解绑: function() { disbindingitem(currentinstance.numiids,this); }, 关闭: function() { $(this).dialog("close"); } } }); } var batchdisbinditeminstance = null; batchdisbindingitem=function() { var isalerted = false; if(batchdisbinditeminstance==null) batchdisbinditeminstance=new batchdisbindingitem(); currentinstance=batchdisbinditeminstance; if(batchdisbinditeminstance.cancelfreeitem()) isalerted = true; batchdisbinditeminstance.setnumiids(); if(isalerted) { if(currentinstance.numiids=="") return; } if(!batchdisbinditeminstance.checkisselected()) return; batchdisbinditeminstance.removedialog(); batchdisbinditeminstance.popupdialog(); } disbindingitem=function(mynumiids,sender) { var urledit = "./x.aspx?method=disbinding&numiids="+mynumiids+"&rdm="+math.random(); $.get(urledit, {}, function (datastring, status, object) { var data = $.evaljson(datastring); ajaxresponseresult(data); if(data.issuccess==true) { $(sender).dialog("close"); $("#listitemdefine").trigger("reloadgrid"); } }); } //____________________________sku批量及单独解除绑定____________________________________________ function batchdisbindingsku(){} myinherits(batchdisbindingsku,batchskumodify); batchdisbindingsku.prototype.cancelfreeitem=function() { var tempnumiids = jquery("#listskudefine").jqgrid('getgridparam','selarrrow'); var tempnumiidsclone = this.clonearr(tempnumiids); var isalert=false; var isbinded = false; for(var i=0;i<tempnumiidsclone.length;i++) { isbinded = false; for(var j=0;j<bindedskuarr.length;j++) { if(tempnumiidsclone[i]==bindedskuarr[j]) { isbinded = true; break; } } if(!isbinded) { isalert = true; jquery("#listskudefine").jqgrid('setselection',tempnumiidsclone[i]); } } if(isalert) { alert("未绑定的sku不能作解绑的动作!"); // 返回true表示已经弹出alert了 return true; } return false; } batchdisbindingsku.prototype.popupdialog=function() { var showstr='<div id="desctempdiv"><span style="color:red;">注意:您正在批量解除sku绑定,如果您解除了绑定,今后数据源宝贝设置有更新您的宝贝设置将不会同步更新!</span></div>'; $(showstr).dialog({ title: "解除绑定", closetext: '关闭', width: 600, height:300, maxheight: $(window).height() * 0.98, minheight: $(window).height() * 0.7, modal: true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 }, buttons: { 解绑: function() { disbindingsku(currentinstance.oneiid,currentinstance.skuids,this); }, 关闭: function() { $(this).dialog("close"); } } }); } var batchdisbindskuinstance = null; // 批量 batchdisbindingsku=function() { var isalerted = false; if(batchdisbindskuinstance==null) batchdisbindskuinstance=new batchdisbindingsku(); currentinstance=batchdisbindskuinstance; if(batchdisbindskuinstance.cancelfreeitem()) isalerted = true; batchdisbindskuinstance.setoneiid(); batchdisbindskuinstance.setskuids(); // 如果已经弹过一次了,那再次判断是否没有勾选的时候,就不要再弹了 if(isalerted) { if(currentinstance.skuids == "") return; } if(!batchdisbindskuinstance.checkisselected()) return; batchdisbindskuinstance.removedialog(); batchdisbindskuinstance.popupdialog(); } // 单独 disbindingsku=function(numiid, skuids, sender) { var urledit = "./x.aspx?method=disbinding&numiid=" + numiid + "&skuids="+skuids+"&rdm=" + math.random(); $.get(urledit, {}, function (datastring, status, object) { var data = $.evaljson(datastring); ajaxresponseresult(data); if (data.issuccess == true) { $(sender).dialog("close"); $("#listskudefine").trigger("reloadgrid"); } }); } })();
以上这篇利用js的闭包原理做对象封装及调用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
推荐阅读
-
利用js的闭包原理做对象封装及调用方法
-
闭包的原理与经典应用场景,访问器属性,类与对象的创建与成员引用,数组与对象的解构过程与经典案例,JS引入到浏览器中的的方法及获取DOM元素的两个API
-
闭包的原理与经典应用场景 、访问器属性、类与对象的创建与成员引用 、数组与对象的解构、JS引入到浏览器中的的方法
-
闭包的原理与经典应用场景,访问器属性,类与对象的创建与成员引用,数组与对象的解构过程与经典案例,JS引入到浏览器中的的方法及获取DOM元素的两个API
-
闭包的原理与经典应用场景 、访问器属性、类与对象的创建与成员引用 、数组与对象的解构、JS引入到浏览器中的的方法
-
利用js的闭包原理做对象封装及调用方法