2013-03-05 81 views
1

嗨,首先對不起我的英文不好。我已經在SO中搜索。但我沒有得到我需要的確切答案。 我的問題是我需要同步Ajax請求。我知道我們可以使用「asynch:false」。 但這會使瀏覽器被鎖定。我有一個文件夾樹(我使用「tafel樹」js)在我的網站。樹節點在運行時生成。每次 用戶單擊一個節點,它將發送請求到服務器並將該節點添加到樹中。 但問題是,如果該頁面點擊F5然後我需要加載我先前已經選擇的樹結構刷新。 我使用「asynch:false」來實現它。但是這會讓瀏覽器太慢。異步AJAX以有序方式調用

,在這裏我有什麼

function loadPage() { 
/* some other codes are here*/ 
/* here i call an ajax for get the inside folder list in correct order.(i am usig  protoype)*/ 
    new Ajax.Request(ajaxGetInsideFolderIdsUrl, 
    { 
    parameters: {branchId: CurrentSelectfolderId}, 
    onSuccess: function(res) { 
      /* onSuccess i call another function*/ 
      var brs = res.responseText.split(","); // branch ids in correct order. 
      syncFolder(brs) 
    } 
} 

function syncFolder(brs){ 
for(var i= 0 ; i < brs.length; i ++){ 
    var tempbranch = tree.getBranchById(brs[i].getId());    
    selectAfterChange(tempbranch) 
    /* 
    selectAfterChange function is used for selecting current branch. calling "tafle tree" select() function in side it. 
    i just created an copy of "select()","_openPopulate()" functions used in "tafle tree" and modified it with "asynch : false ".and now its working fine. 
*/ 
} 
} 
function selectAfterChange(brs){ 
    brs.chk_select(); 
    /* for selecting the branch (created a copy of current "select()" function used in "tafle tree" js 
    and modified it with "asynch : false "and now its working fine.) */ 
    showList();// for listing items in side that folder (in another Ajax page). 
} 

我的問題是,如果用戶打開一個長枝。 然後刷新頁面會因爲同步Ajax調用而花費太多時間加載。 花太多時間對我來說不是一個大問題。但瀏覽器被鎖定,直到所有的請求執行。 有沒有其他方法可以做到這一點。

+0

無論您需要使用localStorage的工作,或讓你的後臺工作,它可以一次返回多個節點。 – epascarello 2013-03-05 03:31:02

回答

0

我不熟悉您使用的樹庫,但在一般情況下,你會解決這個問題的方法是將存儲在瀏覽器中當前展開了路徑(S)(本地存儲,或餅乾,或無論何地)以及刷新頁面時,只加載可見的節點。

比方說,用戶目前正在尋求路徑一/二/三/四。

["one", "one/two", "one/two/three"] 

:在頁面加載時再次,您創建的路徑由一個從後端要求,通過拆分的路徑,並追加的路徑之一的部件隊列您保存路徑的某處,然後然後,您發送「one」的AJAX請求,並在返回結果時,更新樹中的該節點,併發送「one/two」請求。當該請求返回時,更新樹併發送「one/two/three」的請求。

根據你的設計,你就可以開始填充更多的異步請求的樹休息...

+0

謝謝您的重播。現在我正在做的是我從URL中獲取哈希值(例如: - url#fId = 5),然後我調用loadPage()函數併發送此ID(請參閱我的問題中的第一個ajax調用)。 – user2130460 2013-03-05 06:25:51