2014-07-14 36 views
1

由於SharePoint工作異步,因此我無法將多個列表中的數據存儲在數組中並稍後訪問它們。SharePoint中的其他API多個列表

我需要使用3個列表,因爲它們包含來自員工,假期等的數據。

請參閱下面的代碼以獲取更多信息。

有沒有更簡單的方法來使用SharePoint和多個列表來獲取數據。我也嘗試與executequeryasync,但我找不到多個列表的工作解決方案。或者將每個列表的值存儲在數組或變量中,並將其用於另一個函數,因爲它是異步的。

$(function() { 
$('#title').html("Inloggen verlofaanvraag"); 
}); 


function inLoggen() { 
var initialen = $('#initialen').val(); 
var wachtwoord = $('#wachtwoord').val(); 

$.ajax({ 
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Lijst werknemers')/Items?$filter=wInitialen eq '" + initialen + "' and wWachtwoord eq '" + wachtwoord + "'", 
    type: "GET", 
    headers: { "accept": "application/json;odata=verbose" }, 
    success: function (data) { 
     var x = data.d.results; 
     var werknemers = data.d.results; 
     for (var i = 0; i < x.length; i++) { 
      rInitialen = x[i].wInitialen; 
      rWachtwoord = x[i].wWachtwoord; 
      rVolledigenaam = x[i].wVolledigenaam; 

     } 

     if (i === 0) { 
      alert("U hebt geen toegang tot deze pagina !"); 
     } 
     else { 
      $('#title').html("Welkom " + rVolledigenaam); 
      $('#inlogform').hide(); 
      persoonlijketellers(werknemers); 
     } 
    }, 
    error: function (xhr) { 
     console.log(xhr.status + ': ' + xhr.statusText); 
    } 
}); 
} 


function persoonlijketellers(werknemers) { 

var rId = werknemers[0].ID; 

$.ajax({ 
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Lijst persoonlijke tellers')/Items?$filter=pWerknemer eq '" + rId + "'", 
    type: "GET", 
    headers: { "accept": "application/json;odata=verbose" }, 
    success: function (data) { 
     var x = data.d.results; 
     var ptellers = data.d.results; 
     for (var i = 0; i < x.length; i++) { 

     } 

     wettelijkeverlofdagen(werknemers, ptellers); 

    }, 
    error: function (xhr) { 
     console.log(xhr.status + ': ' + xhr.statusText); 
    } 
}); 
} 

function wettelijkeverlofdagen(werknemers, ptellers) { 

var rId = ptellers[0].ID; 

alert(rId); 

$.ajax({ 
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Lijst persoonlijke tellers')/Items?$filter=pWerknemer eq '" + rId + "'", 
    type: "GET", 
    headers: { "accept": "application/json;odata=verbose" }, 
    success: function (data) { 
     var x = data.d.results; 
     var ptellers = data.d.results; 
     for (var i = 0; i < x.length; i++) { 

     } 

    }, 
    error: function (xhr) { 
     console.log(xhr.status + ': ' + xhr.statusText); 
    } 
}); 
} 

回答

0

可以存儲來自多個列表中的數據在陣列和訪問他們的時候所有的異步調用的是完整的,你只需要使用某種形式的承諾的模式。

jQuery的。當方法可能是在這樣的情況下最有用:

function SPData() { 
    function getJsonDataAsync(url) { 
     // returning the $.ajax object is what makes the next part work... 
     return $.ajax({ 
      url: url, 
      method: "GET", 
      contentType: "application/json", 
      headers: { 
       accept: "application/json;odata=verbose" 
      } 
     }); 
    } 

    var requestURI1 = _spPageContextInfo.webServerRelativeUrl + "/_api/lists/..." 
    var requestURI2 = _spPageContextInfo.webServerRelativeUrl + "/_api/lists/..." 
    var requestURI3 = _spPageContextInfo.webServerRelativeUrl + "/_api/lists/..."    
    var req1 = getJsonDataAsync(requestURI1); 
    var req2 = getJsonDataAsync(requestURI2); 
    var req3 = getJsonDataAsync(requestURI3); 
    // now we can do the next line, because req1/2/3 are actually deferreds 
    // being returned from $.ajax 
    jQuery.when(req1, req2, req3).done(function(resp1, resp2, resp3) { 
     /* do something with all of the requests here... 
      resp1/2/3 correspond to the responses from each call and are each an 
      array that looks like: [data, statusText, jqXHR], which means that your 
      data is in resp1[0], resp2[0], etc. */ 
    }); 

如果你願意,你也可以直接將返回的值的變量在一個較高的水平範圍內,然後使用單獨的jQuery推遲,以便您可以確保在開始處理數據之前,所有呼叫都已成功...

... 
    var x1, x2, x3;    
    // use the .then(function() { ... }) pattern because we are just returning a 
    // deferred/promise from $.ajax 
    getJsonDataAsync(requestURI1).then(function(data) { 
     x1 = data; 
     getJsonDataAsync(requestURI2).then(function(data2) { 
      x2 = data2; 
      getJsonDataAsync(requestURI3).then(function(data3) { 
       x3 = data3; 
       // do something with x1, x2, and x3 
      }); 
     }); 
    });  
}