2013-03-22 92 views
2

如何獲取變量,網址和名稱的fileDoesNotExist回調:變量傳給回調函數

window.checkIfFileExists = function(path, url, name) { 
    return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) { 
    return fileSystem.root.getFile(path, { 
     create: false 
    }, fileExists, fileDoesNotExist); 
    }), getFSFail); 
}; 

fileDoesNotExist = (fileEntry, url, name) -> 
    downloadImage(url, name) 

回答

2

的PhoneGap的getFile功能有兩個回調函數。你在這裏所犯的錯誤,fileDoesNotExist是它應該調用兩個函數,而不是引用一個變量。

像下面將工作:

window.checkIfFileExists = function(path, url, name) { 
    return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) { 
    return fileSystem.root.getFile(path, { 
     create: false 
    }, 
    function(e) { 
     //this will be called in case of success 
    }, 
    function(e) { 
     //this will be called in case of failure 
     //you can access path, url, name in here 
    }); 
    }), getFSFail); 
}; 
+0

感謝,那很簡單! :-) – Harry 2013-03-22 14:32:07

1

你可以傳遞一個匿名函數,並在回調的通話將其加入:

window.checkIfFileExists = function(path, url, name) { 
    return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) { 
    return fileSystem.root.getFile(path, { 
     create: false 
    }, fileExists, function(){ 
     //manually call and pass parameters 
     fileDoesNotExist.call(this,path,url,name); 
    }); 
    }), getFSFail); 
};