2

我正在使用 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");
    })

}

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

函数 appendFile(f, textToWrite) {}

但是我似乎无法让它发挥作用。如果我更改回调以包含附加参数,则会收到错误消息。

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

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

蒂姆

4

2 回答 2

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

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

于 2013-04-10T11:08:05.710 回答
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");
于 2013-04-10T12:30:20.090 回答