2017-02-22 56 views
0

Moin,Cordova/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); 
}); 

Где эта ошибка?

Заранее благодарен!

Jan

ответ

0

У меня есть решение:

я должен связать это в методе обратного вызова:

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

Сейчас он работает :)

 Смежные вопросы

  • Нет связанных вопросов^_^