2017-05-10 46 views
0

我正在創建一個在Chrome WebServer AddOn上運行的瀏覽器工具。服務器通過使用GET以JSON數組返回的請求提供目錄文件列表(包括isFolder/isFile /文件名值)

我有一個文件夾列表,我需要掃描此文件夾中的所有文件,包括所有文件應該在pushArray中返回的子目錄。

現在通常我會使用遞歸函數來遍歷這個。正如我使用$.get我有一個異步請求,我不知道如何迭代,然後返回所有文件的列表。我嘗試了.when().done(),但我認爲我只是缺乏這個想法而不是遞歸。

下面的代碼不工作,我明白爲什麼,但我迄今所做的:

function recursiveFileRead(data, pushArray) { 
    var val = data; //data contains 
    val = String(val).trim() + "?json=1" //required so the server gives back the json object 

    //get list of files and folder in a json object (async!) 
    var callBack = $.get(val, function(val) { 
    json = val; 

    //loop through json object. 
    $.each(json, function(i, value) { 
     //if file push to array 
     if(value.isFile) { 
     pushArray(value.filename); 
     } else { 
     //if folder recursively run through the subfolders 
     recursiveFileRead(data, pushArray) 
     }; 
    }); 
    }); 

    $.when(callBack()).done(function() { 
    //return pushArray 
    }); 

} 

後數組包含列出的所有文件,我需要執行的操作,以ZIP的文件。我想我無法做到這一點同步,但如果我異步迭代我怎麼知道我什麼時候完成?

非常感謝!

+0

沒有人有什麼想法? –

回答

0

所以我有一個(非常糟糕的)解決方法。我仍然無法「遞歸地」傳遞一個變量(以便將新數據添加到一個數組中),所以我使用了一個全局變量。 另外,當所有的承諾都解決了的時候,我不會開火,所以我現在正在使用一個計時器。至少我得到我想要的數據,但任何幫助表示讚賞:

首先在我的「常規代碼」我循環通過包含文件/文件夾列表來檢查文件夾。如果有一個文件夾,我打電話了「遞歸」異步功能:

//go through items listed and check if file/folder 
    $.each(data, function(i, value) { 
    //get file ending 
    var extn = value.split(".").pop(); 

    //use array defined before to check 
    if (validFormats.indexOf(extn) == -1) { 
     console.log("folder: " + value); 
     //folder - start recursive directory list 
     promises.push(requestAll(value)); 
    } else { 
     pushArray.push(value); 
     console.log("file: " + value); 
    } 
    }); 

    console.log(promises); //<= logs the promises 
    $.when.apply($, promises).done(function(responses) { 
    alert("done"); //<= doesn't fire 
    console.log(responses); 
    }); 

    setTimeout(function() { 
    alert(pushArrayGlobal.join("\n")) 
    }, 3000); 

正如你可以在註釋中看到,對於諾言的when.apply.done不fireing,超時不和我得到我需要的數據的完整列表。

上市文件下面的代碼「遞歸」是基於https://stackoverflow.com/a/23355670/4178195

function request(data) { 
    // return the AJAX promise 
    return $.ajax({ 
    url: data + '/?json=1', 
    method: 'GET', 
    dataType: 'json' 
    }); 
} 


function requestOddsFrom(folder, items) { 

    return request(folder).then(function(data) { 
    $.each(data, function(i, value) { 
     console.log(value); 
     var newArray = []; 
     console.log(value.name); 
     //if folder 
     if (value.isDirectory) { 
     //remove first folder of relative path and call request again 
     requestOddsFrom(value.fullPath.split('/').slice(2).join('/'), []); 
     //if file add to pushArrayGlobal 
     } else { 
     pushArrayGlobal.push(value.fullPath); 
     } 
    }); 
    }); 
} 

function requestAll(data) { 
    return $.Deferred(function() { 
    var self = this; 
    $.when(requestOddsFrom(data, [])).then(function(items) { 
     self.resolve(pushArrayGlobal); 
    }); 
    }); 
} 
相關問題