如何在javascript类函数上使用回调

时间:2013-10-09 12:22:47

标签: javascript jquery ajax callback

如何在javascript类函数上起诉java脚本回调。我必须在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();
    }
};

2 个答案:

答案 0 :(得分:0)

我已添加,请验证并告诉我。

$(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();
    }
};

答案 1 :(得分: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>