2013-03-22 59 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); 
};