$ .Deferred.pipeをカスタムメソッドで拡張して、遅延チェーンを少し短縮できるようにします。
私が現在持っているコードはこれです
getDeferredFileEntry()
//returns a ($.Deferred) promise to resolve with a FileEntry object
.pipe(function(entry){
//returns a promise to resolve with an object
//containing the contents of the file as text
//and a reference to the file's FileEntry
var def = $.Deferred();
entry.getDeferredText()
.done(function(fileText){
def.resolve({text:fileText, fileEntry:entry});
});
return def.promise();
)}
.done(function(response){
var text = response.text;
var fileEntry = response.fileEntry;
console.log(text);
//do something with the text
console.log(fileEntry);
//do something else with the file entry after finished reading from it
//e.g. deleting with something like fileEntry.remove();
});
短縮したいのですが
getDeferredFileEntry()
.read(
//uses the FileEntry object resolved by getDeferredFileEntry
//to call an asynchronous fileEntry.read() *in the background*
//the contents are then passed to the callback taken from below
//returns promise to resolve with the fileEntry object for chaining
function callback(text){
//do something with the text
console.log(text);
}
)
.remove(
function(fileEntry){
//library call to remove fileEntry that read() promised
}
)
解決されたFileEntryオブジェクトをバックグラウンドgetDeferredFileEntry()
でカスタムread()
に渡す方法に苦労しています。アドバイスをいただければ幸いです