From 47ca081f36cc9af773f252613193025ac4116b9e Mon Sep 17 00:00:00 2001 From: Mark Darbyshire Date: Tue, 15 Feb 2011 18:44:06 +1300 Subject: [PATCH] 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. --- framework/assets/js/storage.js | 36 +++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/framework/assets/js/storage.js b/framework/assets/js/storage.js index 07876659..b47bd670 100755 --- a/framework/assets/js/storage.js +++ b/framework/assets/js/storage.js @@ -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;