mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 12:03:28 +08:00
Merge branch 'localStorage' of https://github.com/ascorbic/phonegap-android into ascorbic-localStorage
This commit is contained in:
commit
87fd9665fe
@ -308,9 +308,71 @@ DroidDB_openDatabase = function(name, version, display_name, size) {
|
|||||||
return db;
|
return db;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For browsers with no localStorage we emulate it with SQLite. Follows the w3c api.
|
||||||
|
* TODO: Do similar for sessionStorage.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var CupcakeLocalStorage = function() {
|
||||||
|
try {
|
||||||
|
|
||||||
|
this.db = openDatabase('localStorage', '1.0', 'localStorage', 2621440);
|
||||||
|
var storage = {};
|
||||||
|
this.db.transaction(
|
||||||
|
function (transaction) {
|
||||||
|
transaction.executeSql('CREATE TABLE IF NOT EXISTS storage (id NVARCHAR(40) PRIMARY KEY, body NVARCHAR(255))');
|
||||||
|
transaction.executeSql('SELECT * FROM storage', [], function(tx, result) {
|
||||||
|
for(var i = 0; i < result.rows.length; i++) {
|
||||||
|
storage[result.rows.item(i)['id']] = result.rows.item(i)['body'];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
},
|
||||||
|
function (err) {
|
||||||
|
alert(err.message);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
this.setItem = function(key, val) {
|
||||||
|
console.log('set');
|
||||||
|
storage[key] = val;
|
||||||
|
|
||||||
|
this.db.transaction(
|
||||||
|
function (transaction) {
|
||||||
|
transaction.executeSql('CREATE TABLE IF NOT EXISTS storage (id NVARCHAR(40) PRIMARY KEY, body NVARCHAR(255))');
|
||||||
|
|
||||||
|
transaction.executeSql('REPLACE INTO storage (id, body) values(?,?)', [key,val]);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.getItem = function(key) {
|
||||||
|
return storage[key];
|
||||||
|
}
|
||||||
|
this.removeItem = function(key) {
|
||||||
|
delete storage[key];
|
||||||
|
this.db.transaction(
|
||||||
|
function (transaction) {
|
||||||
|
transaction.executeSql('CREATE TABLE IF NOT EXISTS storage (id NVARCHAR(40) PRIMARY KEY, body NVARCHAR(255))');
|
||||||
|
|
||||||
|
transaction.executeSql('DELETE FROM storage where id=?', [key]);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch(e) {
|
||||||
|
alert("Database error "+e+".");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
PhoneGap.addConstructor(function() {
|
PhoneGap.addConstructor(function() {
|
||||||
if (typeof window.openDatabase == "undefined") {
|
if (typeof window.openDatabase == "undefined") {
|
||||||
navigator.openDatabase = window.openDatabase = DroidDB_openDatabase;
|
navigator.openDatabase = window.openDatabase = DroidDB_openDatabase;
|
||||||
window.droiddb = new DroidDB();
|
window.droiddb = new DroidDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof window.localStorage == "undefined") {
|
||||||
|
navigator.localStorage = window.localStorage = new CupcakeLocalStorage();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user