Skip to content

Instantly share code, notes, and snippets.

@tateisu
Created September 9, 2013 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tateisu/6500444 to your computer and use it in GitHub Desktop.
Save tateisu/6500444 to your computer and use it in GitHub Desktop.
function formatFileError(err){
var sb = err.toString();
for( var k in err ){
sb += "\n "+k+"="+err[k];
if( k == "code" ){
sb+=" ";
switch (err[k]) {
case FileError.QUOTA_EXCEEDED_ERR: sb += 'QUOTA_EXCEEDED_ERR'; break;
case FileError.NOT_FOUND_ERR: sb += 'NOT_FOUND_ERR'; break;
case FileError.SECURITY_ERR: sb += 'SECURITY_ERR'; break;
case FileError.INVALID_MODIFICATION_ERR: sb += 'INVALID_MODIFICATION_ERR'; break;
case FileError.INVALID_STATE_ERR: sb += 'INVALID_STATE_ERR'; break;
};
}
}
return sb;
}
if( ! Function.prototype.bind ){
Function.prototype.bind = function(oThis){
if( typeof this !== "function" ){
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1)
,fToBind = this
,fNOP = function () {}
,fBound = function () {
return fToBind.apply(
(this instanceof fNOP && oThis ? this : oThis)
, aArgs.concat(Array.prototype.slice.call(arguments))
);
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
function testFileSave2(){
// テスト用パラメータ
var file_name = "file1";
var file_option = {create:true};
var file_data = JSON.stringify({ key1: 'v1',key2: 'v2',key3: 'v3'});
var quota = 8 * 1024 * 1024;
var bPersistent = true;
//
// Tizen 2.2 には window.webkitStorageInfo も window.storageInfo もないようだ
// APIの名称がwebkitだとちょっと違う
var requestFileSystem = ( window.webkitRequestFileSystem || window.requestFileSystem );
var mode = (bPersistent ? window.PERSISTENT : window.TEMPORARY );
//
Deferred.next(function(){
var d = new Deferred();
requestFileSystem(mode,quota, function(fs){
d.call( fs );
},d.fail.bind(d));
return d;
// success FileSystem or fail FileError
}).next(function(fs){
var d = new Deferred();
fs.root.getFile( file_name,file_option, d.call.bind(d), d.fail.bind(d) );
return d;
// success FileEntry or fail FileError
}).next(function(entry){
var d = new Deferred();
entry.createWriter( d.call.bind(d), d.fail.bind(d) );
return d;
// success FileWriter or fail FileError
}).next(function(writer){
var d = new Deferred();
writer.onwriteend = d.call.bind(d);
writer.onerror = d.fail.bind(d);
writer.write(new Blob([ file_data ]));
return d;
// success undefined or fail FileError
}).next(function(){
console.log("write complete!");
}).error(function(err){
console.log( formatFileError(err) );
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment