mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 23:42:53 +08:00
Implement localStorage.key() and localStorage.length
This brings PhoneGap's implementation in line with the spec at http://dev.w3.org/html5/webstorage/ It makes the following demo work when you include PhoneGap: http://people.w3.org/mike/localstorage.html I was hopeful it would make my app, which makes use of LawnChair, work, but I've had no such luck as of yet.
This commit is contained in:
parent
939b70243d
commit
47ca081f36
@ -307,15 +307,21 @@ var CupcakeLocalStorage = function() {
|
||||
|
||||
this.db = openDatabase('localStorage', '1.0', 'localStorage', 2621440);
|
||||
var storage = {};
|
||||
this.length = 0;
|
||||
function setLength (length) {
|
||||
this.length = length;
|
||||
localStorage.length = length;
|
||||
}
|
||||
this.db.transaction(
|
||||
function (transaction) {
|
||||
var i;
|
||||
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(i = 0; i < result.rows.length; i++) {
|
||||
storage[result.rows.item(i).id] = result.rows.item(i).body;
|
||||
for(var i = 0; i < result.rows.length; i++) {
|
||||
storage[result.rows.item(i)['id']] = result.rows.item(i)['body'];
|
||||
}
|
||||
PhoneGap.initializationComplete("cupcakeStorage");
|
||||
setLength(result.rows.length);
|
||||
PhoneGap.initializationComplete("cupcakeStorage");
|
||||
});
|
||||
|
||||
},
|
||||
@ -324,13 +330,13 @@ var CupcakeLocalStorage = function() {
|
||||
}
|
||||
);
|
||||
this.setItem = function(key, val) {
|
||||
//console.log('set');
|
||||
if (typeof(storage[key])=='undefined') {
|
||||
this.length++;
|
||||
}
|
||||
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]);
|
||||
}
|
||||
);
|
||||
@ -340,24 +346,36 @@ var CupcakeLocalStorage = function() {
|
||||
};
|
||||
this.removeItem = function(key) {
|
||||
delete storage[key];
|
||||
this.length--;
|
||||
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]);
|
||||
}
|
||||
);
|
||||
};
|
||||
this.clear = function() {
|
||||
storage = {};
|
||||
this.length = 0;
|
||||
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', []);
|
||||
}
|
||||
);
|
||||
};
|
||||
this.key = function(index) {
|
||||
var i = 0;
|
||||
for (var j in storage) {
|
||||
if (i==index) {
|
||||
return j;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch(e) {
|
||||
alert("Database error "+e+".");
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user