【问题标题】:Cordova/Javascript: method as callback functionCordova/Javascript:作为回调函数的方法
【发布时间】:2017-07-11 20:22:04
【问题描述】:

莫恩,

我觉得有点傻。我尝试将以下函数转换为对象的方法:

function listDir(path){
  window.resolveLocalFileSystemURL(path,
    function (fileSystem) {
      var reader = fileSystem.createReader();
      reader.readEntries(
        function (entries) {
          console.log(entries);
        },
        function (err) {
          console.log(err);
        }
      );
    }, function (err) {
      console.log(err);
    }
  );
}

来源:Cordova list all files from application directory (WWW)

但是我尝试的任何方式都行不通。我不知道如何将这些方法用作回调函数,尤其是如何设置函数的参数。使用我的代码,我期望输出:

debug: listdir
debug: filesystem
debug: entries
[Array or something]

但我只是得到:

debug: listdir
debug: filesystem

这是我的代码:

function Filelist() {}

Filelist.prototype = {
    methodErr: function (err) {
        console.log('debug: error');
        console.log(err);
    },
    methodEntries: function (entries) {
        console.log('debug: entries');
        console.log(entries);
    },
    methodFilesystem: function (fileSystem) {
        console.log('debug: filesystem');
        var reader = fileSystem.createReader();
        reader.readEntries(this.methodEntries, this.methodErr);
    },
    methodListDir: function (path) {
        console.log('debug: listdir');
        window.resolveLocalFileSystemURL(
            path,
            this.methodFilesystem,
            this.methodErr
        );
    }
}


fl = new Filelist();
$('.klicken').click(function () {
    fl.methodListDir(cordova.file.applicationDirectory);
});

错误在哪里?

提前致谢!

一月

【问题讨论】:

    标签: javascript cordova methods callback arguments


    【解决方案1】:

    我有办法:

    我必须在回调方法中绑定这个:

    ...    
        reader.readEntries(this.methodEntries.bind(this), this.methodErr.bind(this));
    ...
        window.resolveLocalFileSystemURL(
            path,
            this.methodFilesystem.bind(this),
            this.methodErr.bind(this)
            );
    ...
    

    现在可以了:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多