Your method saveshould work fine (basically, see below) for your second requirement (write to the name of the selected code without the username), but there are a few errors in open(at least as presented in the question):
- In the callback
chooseEntry this !== fileHandler, because the callback is called using another this(possibly background pages window).
You can get around this in several ways:- Use
fileHandlerinstead this(if you are not using it as any prototype). .bind(this) .var self = this; open self.entry (et cetera) .
cb . , (,) fileHandler.save ( - ?), -
⋮
cb && cb(self.entry);
⋮
self.entry = dirEntry () open save:
fileHandler.open(function(ent) {
fileHandler.save('newfile','This is the text\nto save in the (possibly) new file.');
});
save : - , writer.truncate() ( , ).
⋮
writer.onwrite = function() {
writer.onwrite = null;
writer.truncate(writer.position);
};
writer.write(…);
⋮
, , . , , ; , ( , readEntries ).
function list_dir(dirent, cb, listing) {
if (listing === undefined) listing = [];
var reader = dirent.createReader();
var read_some = reader.readEntries.bind(reader, function(ents) {
if (ents.length === 0)
return cb && cb(listing);
process_some(ents, 0);
function process_some(ents, i) {
for(; i < ents.length; i++) {
listing.push(ents[i]);
if (ents[i].isDirectory)
return list_dir(ents[i], process_some.bind(null, ents, i + 1), listing);
}
read_some();
}
}, function() {
console.error('error reading directory');
});
read_some();
}
open ( , ) :
fileHandler.open(function(ent) {
ent && list_dir(ent, function(listing) {
fileHandler.listing = listing;
console.log('listing', fileHandler.listing.map(function(ent){return ent.fullPath}).join('\n'));
fileHandler.save('a_dir/somefile','This is some data.');
});
});