2013-03-22 3 views
2

Как получить переменные, URL и имя для обратного вызова 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

getFile Функция phoneGap имеет две функции обратного вызова. Ошибка, которую вы здесь делаете, с 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

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); 
}; 
Смежные вопросы