forked from 0x2620/oxjs
fix some localStorage bugs
This commit is contained in:
parent
c9d2ac5fcd
commit
64592e7278
4 changed files with 17 additions and 9 deletions
|
|
@ -127,31 +127,39 @@ Ox.load = function() {
|
|||
|
||||
/*@
|
||||
Ox.localStorage <o> localStorage wrapper
|
||||
() -> <o> key:value pairs
|
||||
(key) -> <*> value
|
||||
(key, val) -> <f> localStorage object
|
||||
({key, val}) -> <f> localStorage object
|
||||
@*/
|
||||
Ox.localStorage = function(namespace) {
|
||||
if (Ox.isUndefined(localStorage)) {
|
||||
// Ox.Log calls this before Ox.isUndefined is defined
|
||||
if (localStorage === void 0) {
|
||||
localStorage = {};
|
||||
}
|
||||
function storage(key, val) {
|
||||
var ret, value;
|
||||
if (Ox.isUndefined(key)) {
|
||||
if (arguments.length == 0) {
|
||||
ret = {};
|
||||
Ox.forEach(localStorage, function(val, key) {
|
||||
if (Ox.startsWith(key, namespace + '.')) {
|
||||
ret[key.substr(namespace.length + 1)] = JSON.parse(val);
|
||||
}
|
||||
});
|
||||
} else if (Ox.isUndefined(val)) {
|
||||
} else if (arguments.length == 1 && !Ox.isObject(key)) {
|
||||
value = localStorage[namespace + '.' + key];
|
||||
ret = Ox.isUndefined(value) ? void 0 : JSON.parse(value);
|
||||
} else {
|
||||
localStorage[namespace + '.' + key] = JSON.stringify(val);
|
||||
args = Ox.makeObject(arguments);
|
||||
Ox.forEach(args, function(val, key) {
|
||||
localStorage[namespace + '.' + key] = JSON.stringify(val);
|
||||
});
|
||||
ret = this;
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
storage.delete = function(key) {
|
||||
var keys = Ox.isUndefined(key)
|
||||
var keys = arguments.length == 0
|
||||
? Object.keys(storage())
|
||||
: [key];
|
||||
keys.forEach(function(key) {
|
||||
|
|
@ -166,7 +174,7 @@ Ox.Log <f> Logging module
|
|||
@*/
|
||||
Ox.Log = (function() {
|
||||
var storage = Ox.localStorage('Ox'),
|
||||
log = storage('Log') || {filter: [], filterEnabled: true};
|
||||
log = storage('Log') || {filter: [], filterEnabled: true},
|
||||
that = function() {
|
||||
var ret;
|
||||
if (arguments.length == 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue