2013-10-09 4 views
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("&region="); 
      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(); 
    } 
}; 
+0

이 읽기, 당신 http://recurial.com/programming/understanding-callback-functions-in-javascript/ – Misters

+2

도움이 될 수 있습니다 .. . 당신의 코드를 상당히 단순화 할 것입니다. –

+0

getParam 및 hadleServerResponse 메소드를 수정할 수 있습니다. 내 편집 된 답변을 살펴보십시오. –

답변

0

내가 추가하여 알려주세요. 당신이 jQuery를 당신이 아약스 기능을 사용할 수 있습니다 사용하고 있기 때문에

$(document).ready (function() 
{ 
TerritoryList.getParam(SalesExceptionNMGList.getParam);//takes 1 minutes to complete 



}); 


    var TerritoryList={ 
     act:null, 
     division:null, 
     region:null, 
     cluster:null, 
     finishHandler = function(){}; 
     getParam:function(handler){ 
      this.finishHandler = handler; 
      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("&region="); 
      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'); 

      finishHandler(); 
     } 
    }; 

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(); 
    } 
}; 
0
<script> 
var TerritoryList={ 
    // Your code 
    getParam:function(callback){ 
     // Your code 
     this.callback = callback; 
    }, 
    handleServerResponse:function(response){ 
     // Your code 
     this.callback(); 
    } 
}; 

TerritoryList.getParam(SalesExceptionNMGList.getParam); 
</script> 
관련 문제