Harry
Harry

Reputation: 13329

Pass variables to function callBack

How do I get the variables, url and name to the fileDoesNotExist callback:

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)

Upvotes: 2

Views: 324

Answers (2)

Tim Ebenezer
Tim Ebenezer

Reputation: 2724

The getFile function of phoneGap has two callback functions. The mistake you are making here, with fileDoesNotExist is that it should be calling to two functions, rather than referencing a variable.

Something like the following would work:

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);
};

Upvotes: 2

Joseph
Joseph

Reputation: 119847

You could pass in an anonymous function, and add them in to the call of the callback:

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);
};

Upvotes: 1

Related Questions