2017-02-22 46 views
0

Moin,科尔多瓦/ Javascript:作为回调函数的方法

我觉得有点愚蠢。我尝试下面的函数转换为对象的方法:

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

哪里错误?

在此先感谢!

回答

0

我已经得到了解决:

我必须在回调方法绑定此:

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

现在工程:)