将附加参数传递给用于Phonegap文件处理的javascript回调

时间:2013-04-10 10:49:02

标签: javascript cordova callback

我正在使用PhoneGap API来处理文件。我正在执行以下调用,该调用将appendfile函数作为回调方法调用。

fileSystem.root.getFile("test.txt", { create: true }, appendFile , onError);

这叫:

function appendFile(f) {

    f.createWriter(function (writerOb) {
        writerOb.onwrite = function () {
            logit("Done writing to file.<p/>");
        }
        //go to the end of the file...
        writerOb.seek(writerOb.length);
        writerOb.write("Test at " + new Date().toString() + "\n");
    })

}

我想更改被调用函数以接受我想要附加到文件的文本的附加参数,例如:

function appendFile(f,textToWrite){}

但是我似乎无法让这个工作。如果我更改回调以包含附加参数,我会收到错误。

fileSystem.root.getFile("test.txt", { create: true }, appendFile(textToWrite) , onError); // doesnt work.

有人可以指出我正确的方向吗....谢谢。

2 个答案:

答案 0 :(得分:1)

你可以尝试一下。

fileSystem.root.getFile("test.txt", { create: true },
 function (f,textToWrite){
 f.createWriter(function (writerOb) {
        writerOb.onwrite = function () {
            logit("Done writing to file.<p/>");
        }
        //go to the end of the file...
        writerOb.seek(writerOb.length);
        writerOb.write("Test at " + new Date().toString() + "\n");
    })
} 
, onError);

注意:我无法测试代码,因为我没有环境

答案 1 :(得分:1)

fileSystem.root.getFile("test.txt", { create: true }, appendFile , onError);
var textToWrite = 'blah blah';

function appendFile(f) {

    f.createWriter(function (writerOb) {
        writerOb.onwrite = function () {
            logit("Done writing to file.<p/>");
        }
        //go to the end of the file...
        writerOb.seek(writerOb.length);
        writerOb.write("Test at " + new Date().toString() + " " + textToWrite + "\n");
    })

}

您还可以添加属性功能,所以

appendFile.textToWrite = 'blah blah';

然后

writerOb.write("Test at " + new Date().toString() + " " + appendFile.textToWrite + "\n");