带有ajax调用的递归函数

时间:2014-02-20 09:57:36

标签: jquery ajax recursion

我的REST API有三列"ser_noofsubconstituents,ContactId,ParentCustomerId"

if ser_noofsubconstituents > 0这意味着他们有孩子接触,那么我们需要递归调用这个函数,ser_noofsubconstituents数量最多为10。

请在jquery中建议任何好的解决方案。

        function getContituent(id, parentcustomerid) {
        var query = "";
        if (parentcustomerid)
            query = _ODataPath() + "ContactSet?$select=ser_noofsubconstituents,FullName,ContactId,ParentCustomerId&$filter=ParentCustomerId/Id eq guid'" + id + "'";
        else
            query = _ODataPath() + "ContactSet" + "(guid'" + id + "')?$select=ser_noofsubconstituents,FullName,ContactId,ParentCustomerId";

        return $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: query,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, xhr) {
                if (!parentcustomerid) {
                    var cont = {};
                    window._noofsubconstituents = data.d.ser_noofsubconstituents;
                    cont.ser_noofsubconstituents = data.d.ser_noofsubconstituents;
                    cont.contactId = data.d.ContactId;
                    cont.FullName = data.d.FullName;
                    cont.ParentCustomerId = data.d.ParentCustomerId;
                    items.push(cont);
                    window.fetchCompleted = true;
                    $("#contactListView").append("<li style='padding-left:4px;' onclick='contituent_click(this)' data-id='" + cont.contactId + "'><h2>" + cont.FullName + "<h2><div></div></li>");
                } else {
                    for (var i = 0, len = data.d.results.length; i < len; ++i) {
                        var cont = {};
                        window._noofsubconstituents = data.d.results[i].ser_noofsubconstituents;
                        cont.ser_noofsubconstituents = data.d.results[i].ser_noofsubconstituents;
                        cont.contactId = data.d.results[i].ContactId;
                        cont.FullName = data.d.results[i].FullName;
                        cont.ParentCustomerId = data.d.results[i].ParentCustomerId;
                        items.push(cont);

                        var pl = $("#contactListView").find("li[data-id=" + cont.ParentCustomerId.Id + "]").attr("style");

                        if (pl != undefined) {
                            pl = parseInt($.trim(pl.split(":")[1].replace("px", ""))) + 4;
                        } else {
                            pl = 4;
                        }

                        $("#contactListView").append("<li style='padding-left:" + pl + "px;' onclick='contituent_click(this)'  data-id='" + cont.contactId + "'><h2>" + cont.FullName + "<h2><div></div></li>");
                    }

                    if (data.d.results.length > 0) {
                        window.fetchCompleted = false;
                        getContituent(data.d.results[0].ContactId, true);
                    }
                }
                if (window._noofsubconstituents > 0) {
                    window.fetchCompleted = false;
                    getContituent(data.d.ContactId, true);
                }
            },
            error: function (xhr, textStatus, errorThrown) { }
        });
    }

1 个答案:

答案 0 :(得分:0)

在此上下文中执行递归有点不同,因为调用是异步的。你想要做的是提供一个回调并从中返回,而不是从函数本身返回。

例如:

function getStuff(inputArray, done, results, index) {
    if (!results) results = [];
    if (index === inputArray.length) return done(results);
    $.ajax({
        url: 'example.com/json',
        success: function(data) {
            results.push(data);
            getStuff(inputArray, done, results, ++index);
        }
    });
}