I have a global var var imageURL = jQuery.Deferred();
as a deferred object.
Next I have a function that runs through a loop, and for each loop I intend to get the value that is produced through an asynchronous function. The problem I had was that I couldnt get the values properly as they were being called before the function had returned their value, so I was told to use jQuery deferred. Can't quite get my head round it:
downloadProductImage: function(remoteImage, localImageName){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(localImageName, {create: true, exclusive: false}, function ff1(fileEntry) {
var localPath = fileEntry.fullPath;
var ft = new FileTransfer();
ft.download(remoteImage,
localPath, function (entry) {
imageURL.resolve(entry.fullPath);
//return entry.fullPath;
}, fail);
}, fail);
}, fail);
}
The downloadProductImage function is executed in a loop, and just after this function is called I wish to get the value of the imageURL.resolve(entry.fullPath), so I did this:
//start loop
imageURL.done(function(theURL){
alert(theURL);
});
//end loop
If I understand this correctly, the callback from the file download is ran, and when completed the imageURL.done() is executed. But the done() keeps showing the same file path, as if it is overwriting each one.
Any help would be appreciated.