2014-02-20 129 views
0

我的REST API有三列"ser_noofsubconstituents,ContactId,ParentCustomerId"與Ajax調用遞歸函數

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) { } 
     }); 
    } 

回答

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