fix wallet ids
This commit is contained in:
parent
4dcd18c42f
commit
d0e114c346
2 changed files with 36 additions and 9 deletions
|
|
@ -145,7 +145,6 @@ WalletFactory.prototype.create = function(config, opts) {
|
||||||
var w = new Wallet(config);
|
var w = new Wallet(config);
|
||||||
w.create(opts);
|
w.create(opts);
|
||||||
w.store();
|
w.store();
|
||||||
this.addWalletId(w.id);
|
|
||||||
return w;
|
return w;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -160,21 +159,22 @@ WalletFactory.prototype.remove = function(walletId) {
|
||||||
|
|
||||||
WalletFactory.prototype.addWalletId = function(walletId) {
|
WalletFactory.prototype.addWalletId = function(walletId) {
|
||||||
var ids = this.getWalletIds();
|
var ids = this.getWalletIds();
|
||||||
if (ids.indexOf(walletId) == -1) return;
|
if (ids.indexOf(walletId) !== -1) return;
|
||||||
storage.set('walletIds', (ids ? ids + ',' : '') + walletId);
|
ids.push(walletId);
|
||||||
|
this.storage.setGlobal('walletIds', ids);
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletFactory.prototype._delWalletId = function(walletId) {
|
WalletFactory.prototype._delWalletId = function(walletId) {
|
||||||
var ids = this.getWalletIds();
|
var ids = this.getWalletIds();
|
||||||
var index = ids.indexOf(walletId);
|
var index = ids.indexOf(walletId);
|
||||||
if (index == -1) return;
|
if (index === -1) return;
|
||||||
ids.splice(index, 1); // removes walletId
|
ids.splice(index, 1); // removes walletId
|
||||||
this.storage.set('walletIds', ids.join(','));
|
this.storage.setGlobal('walletIds', ids);
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletFactory.prototype.getWalletIds = function() {
|
WalletFactory.prototype.getWalletIds = function() {
|
||||||
var ids = this.storage.get('walletIds');
|
var ids = this.storage.getGlobal('walletIds');
|
||||||
return ids ? ids.split(',') : [];
|
return ids || [];
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.factory = new WalletFactory();
|
Wallet.factory = new WalletFactory();
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,42 @@ function Storage() {
|
||||||
this.data = {};
|
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) {
|
Storage.prototype._key = function(walletId, k) {
|
||||||
return walletId + '::' + k;
|
return walletId + '::' + k;
|
||||||
};
|
};
|
||||||
// get value by key
|
// get value by key
|
||||||
Storage.prototype.get = function(walletId, k) {
|
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
|
// set value for key
|
||||||
Storage.prototype.set = function(walletId, k,v) {
|
Storage.prototype.set = function(walletId, k,v) {
|
||||||
localStorage.setItem(this._key(walletId,k), JSON.stringify(v));
|
localStorage.setItem(this._key(walletId,k), JSON.stringify(v));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue