0
자바 스크립트 클래스 함수에서 자바 스크립트 콜백을 고소하는 방법. TerritoryList.getParam(); 후에 SalesExceptionNMGList.getParam()을 호출해야합니다. 콜백 만 사용하여 완료합니다. 문제를 해결하기 위해 제안하십시오.자바 스크립트 클래스 함수에서 콜백을 사용하는 방법
$(document).ready (function()
{
TerritoryList.getParam();//takes 1 minutes to complete
SalesExceptionNMGList.getParam();//call when TerritoryList.getParam() completes and get ajax response. using callback
});
var TerritoryList={
act:null,
division:null,
region:null,
cluster:null,
getParam:function(){
this.act='getTerritory';
this.division = $("#division").val();
this.region = $("#region").val();
this.cluster = $("#cluster").val();
var TA = [];
TA.push("act=");
TA.push(this.act);
TA.push("&division=");
TA.push(this.division);
TA.push("®ion=");
TA.push(this.region);
TA.push("&cluster=");
TA.push(this.cluster);
resetHierarchy("#territory,#wssTerritory,#freeWssTerritory,#territorycount,#wsscount");
if(this.cluster != "-1"){
this.callToServer(TA.join(""));
}
},
callToServer:function(arg){
var a="FillHeirarchy.do?"+arg;
AJAX.setParams("POST",a,"text");
$("#loader").show();
AJAX.mXHTTPRequest(AJAX,this.handleServerResponse,true);
return false
},
handleServerResponse:function(response){
if (typeof callback === "function") {
callback(response);
}
var resultobject=eval("("+response+")");
var res=resultobject.list;
var h = "#territory";
var TA = [];
TA.push("<option value='-1' selected='true'>Select Territory</option>");
TA.push("<option value='0' > All </option>");
if(res.length>0)
{
if(res.length == 1)
{
var divisionVal=res[0];
var divisionArr=divisionVal.split("$");
TA.push("<option value='"+divisionArr[0]+"' title='"+divisionArr[1].capitalize()+"'>"+divisionArr[1].toUpperCase()+"</option>");
}
else
{
for (var i=0; i<res.length; i++)
{
divisionVal=res[i];
divisionArr=divisionVal.split("$");
TA.push("<option value='"+divisionArr[0]+"' title='"+divisionArr[1].capitalize()+"'>"+divisionArr[1].toUpperCase()+"</option>");
}
}
$(h).html(TA.join(""));
// WssTerritoryList.getParam();
delete (TA);
}
$("#loader").hide();
$("#territorycount").html(res.length);
selectedAll1('territory');
}
};
var SalesExceptionNMGList={
act:null,
division:null,
region:null,
cluster:null,
territory:null,
wssTerritory:null,
searchType:null,
exception:null,
getParam:function(){
this.act='salesExceptionNMGList';
this.division = $("#division").val();
this.region = $("#region").val();
this.cluster = $("#cluster").val();
this.territory = $("#territory").val();
this.wssTerritory = $("#wssTerritory").val();
alert("wss= "+this.wssTerritory);
alert("territory= "+this.territory);
alert("cluster= "+this.cluster);
if (this.wssTerritory!="-1") {
this.searchType="wss";
} else if (this.territory!="-1") {
this.searchType="territory";
} else if (this.cluster!="-1") {
this.searchType="cluster";
} else{
this.searchType="cluster";
}
if($('#chkSEOPG').is(':checked')){
this.exception="OPG";
}else if($('#chkSEPLB').is(':checked')){
this.exception="PLB";
}else{
this.exception="-1";
}
var TA = [];
TA.push("act=");
TA.push(this.act);
TA.push("&division=");
TA.push(this.division);
TA.push("&searchType=");
TA.push(this.searchType);
TA.push("&salesException=");
TA.push(this.exception);
// resetHierarchy("#freeWssTerritory");
this.callToServer(TA.join(""));
},
callToServer:function(arg){
var a="FillHeirarchy.do?"+arg;
alert(a);
AJAX.setParams("POST",a,"text");
$("#loader").show();
AJAX.mXHTTPRequest(AJAX,this.handleServerResponse,true);
return false
},
handleServerResponse:function(response){
alert(response);
var resultobject=eval("("+response+")");
var res=resultobject.list;
var h = "#product_nmg";
var TA = [];
TA.push("<option value='-1' selected='true'>Select NMG</option>");
TA.push("<option value='0' >All</option>");
if(res.length == 1)
{
var divisionVal=res[0];
var divisionArr=divisionVal.split("$");
TA.push("<option value='"+divisionArr[0]+"' title='"+divisionArr[1].capitalize()+"'>"+divisionArr[1].capitalize()+"</option>");
}
else
{
for (var i=0; i<res.length; i++)
{
divisionVal=res[i];
divisionArr=divisionVal.split("$");
TA.push("<option value='"+divisionArr[0]+"' title='"+divisionArr[1].capitalize()+"'>"+divisionArr[1].capitalize()+"</option>");
}
}
$(h).html(TA.join(""));
// ClusterList.getParam();
delete (TA);
$("#loader").hide();
}
};
이 읽기, 당신 http://recurial.com/programming/understanding-callback-functions-in-javascript/ – Misters
도움이 될 수 있습니다 .. . 당신의 코드를 상당히 단순화 할 것입니다. –
getParam 및 hadleServerResponse 메소드를 수정할 수 있습니다. 내 편집 된 답변을 살펴보십시오. –