From d0e114c3462a9ddd3242ce7b72e052fe25f8f179 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 15 Apr 2014 13:21:14 -0300 Subject: [PATCH] fix wallet ids --- js/models/core/Wallet.js | 14 +++++++------- js/models/storage/Plain.js | 31 +++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 13378f66f..a5c6eb527 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -145,7 +145,6 @@ WalletFactory.prototype.create = function(config, opts) { var w = new Wallet(config); w.create(opts); w.store(); - this.addWalletId(w.id); return w; }; @@ -160,21 +159,22 @@ WalletFactory.prototype.remove = function(walletId) { WalletFactory.prototype.addWalletId = function(walletId) { var ids = this.getWalletIds(); - if (ids.indexOf(walletId) == -1) return; - storage.set('walletIds', (ids ? ids + ',' : '') + walletId); + if (ids.indexOf(walletId) !== -1) return; + ids.push(walletId); + this.storage.setGlobal('walletIds', ids); }; WalletFactory.prototype._delWalletId = function(walletId) { var ids = this.getWalletIds(); var index = ids.indexOf(walletId); - if (index == -1) return; + if (index === -1) return; ids.splice(index, 1); // removes walletId - this.storage.set('walletIds', ids.join(',')); + this.storage.setGlobal('walletIds', ids); }; WalletFactory.prototype.getWalletIds = function() { - var ids = this.storage.get('walletIds'); - return ids ? ids.split(',') : []; + var ids = this.storage.getGlobal('walletIds'); + return ids || []; }; Wallet.factory = new WalletFactory(); diff --git a/js/models/storage/Plain.js b/js/models/storage/Plain.js index 04c3f2641..bd1493fe2 100644 --- a/js/models/storage/Plain.js +++ b/js/models/storage/Plain.js @@ -6,15 +6,42 @@ function Storage() { this.data = {}; } +Storage.prototype._read = function(k) { + var ret; + try { + ret = JSON.parse(localStorage.getItem(k)); + } catch (e) {}; + return ret; +}; + + +// get value by key +Storage.prototype.getGlobal = function(k) { + return this._read(k); +}; + +// set value for key +Storage.prototype.setGlobal = function(k,v) { + localStorage.setItem(k, JSON.stringify(v)); +console.log('[Plain.js.25]',k,v); //TODO + +}; + +// remove value for key +Storage.prototype.removeGlobal = function(k) { + localStorage.removeItem(k); +}; + + + Storage.prototype._key = function(walletId, k) { return walletId + '::' + k; }; // get value by key Storage.prototype.get = function(walletId, k) { - return JSON.parse(localStorage.getItem(this._key(walletId,k))); + return this._read(localStorage.getItem(this._key(walletId,k))); }; - // set value for key Storage.prototype.set = function(walletId, k,v) { localStorage.setItem(this._key(walletId,k), JSON.stringify(v));