在一个模板中合并 ajax 个请求

Combine ajax requests in one template

我有 2 个 Ajax 请求获取一些 CRM 详细信息,首先我想获取系统中的所有 订单状态 创建一个 ID 为每个状态并从另一个请求中获取所有订单,然后将它们放入每个状态 ID 容器

发现很难遍历状态和订单并相应地放置它们。

代码

function createOrderStatuses(){
  var access_token = BCAPI.Helper.Site.getAccessToken();
  var request = $.ajax({
      url: "/webresources/api/v3/sites/current/orderstatuses",
      type: "GET",
      connection: "keep-alive",    
      contentType: "application/json",
      headers: _authorization
  });
  request.done(function (msg) {
      console.log(msg);
      //var orderStatusesArray = msg[];
      for (var i = 0; i < msg.items.length; i++){
           var statuses = msg.items[i];
           var statusTemplate = '<div class="large-12 columns margin-distributed statusClass" id="'+ statuses.id +'"><h4 class="lato-bold">' + statuses.label  + '</h4></div>';

      if (statuses.label !== "EXCHANGE FEE PAID"){
        $("#ordersContainer").append(statusTemplate);
      }else{
        //$(".orderStatuses").append('<option value="'+ statuses.id +'">' + statuses.label  + '</option>');
      }
    }//looping and displaying statuses

   getOrders();

  });//request END


  function getOrders(){

     var access_token = BCAPI.Helper.Site.getAccessToken();
     var request = $.ajax({
         url: "/webresources/api/v3/sites/current/orders",
         type: "GET",
         connection: "keep-alive",    
         contentType: "application/json",
         headers: _authorization
     });
     request.done(function (msg) {
         console.log(msg);
         var containerID = $('.statusClass').each(function(){ $(this).attr('id'); });

         for (var i = 0; i < msg.items.length; i++){
           var orders = msg.items[i];

           var orderTemplate = '<div class="large-12 columns margin-distributed '+ orders.statusTypeId +'"><h5>' + orders.name  + '</h5></div>';
           var orderTemplateClass = orderTemplate.find('div[class*="'+orders.statusTypeId+'"]');

           if  ( orderTemplateClass === containerID ){          
                       $(containerID).append(orderTemplate); 
               }// end IF       

    }//looping orders

     }); 
  }//GET ORDERS END


 }
 createOrderStatuses();

您可以 select 目标状态容器 div 这样:$("#ordersContainer .statusClass[id="+orders.statusTypeId+"]")

这里是 getOrders() 函数的完整代码:

function getOrders(){

     var access_token = BCAPI.Helper.Site.getAccessToken();
     var request = $.ajax({
         url: "/webresources/api/v3/sites/current/orders",
         type: "GET",
         connection: "keep-alive",    
         contentType: "application/json",
         headers: _authorization
     });
     request.done(function (msg) {
         console.log(msg);

         for (var i = 0; i < msg.items.length; i++){
           var orders = msg.items[i];

           var orderTemplate = '<div class="large-12 columns margin-distributed '+ orders.statusTypeId +'"><h5>' + orders.name  + '</h5></div>';


    $("#ordersContainer .statusClass[id="+orders.statusTypeId+"]").append(orderTemplate)



    }//looping orders

     }); 
  }//GET ORDERS END


 }