forked from 0x2620/oxjs
add localStorage to polyfills; slightly refactor Ox.localStorage; remove whitespace; update example
This commit is contained in:
parent
41a3eaacd4
commit
5b6f161612
3 changed files with 74 additions and 28 deletions
|
|
@ -83,7 +83,7 @@ Some conventions:
|
|||
@*/
|
||||
global.Ox = function(value) {
|
||||
return Ox.wrap(value);
|
||||
};
|
||||
};
|
||||
})(this);
|
||||
|
||||
/*@
|
||||
|
|
@ -139,8 +139,6 @@ Ox.load = function() {
|
|||
/*@
|
||||
Ox.localStorage <f> localStorage wrapper
|
||||
(namespace) -> storage <f> localStorage function for a given namespace
|
||||
FIXME: there is a bug in Ox.doc here,
|
||||
will use "(namespace)" as function name
|
||||
() -> <o> returns all key:value pairs
|
||||
(key) -> <*> returns one value
|
||||
(key, val) -> <f> sets one value, returns localStorage object
|
||||
|
|
@ -151,7 +149,7 @@ Ox.localStorage = function(namespace) {
|
|||
window.localStorage = {};
|
||||
}
|
||||
function storage(key, value) {
|
||||
var args, ret;
|
||||
var ret;
|
||||
if (arguments.length == 0) {
|
||||
ret = {};
|
||||
Ox.forEach(localStorage, function(value, key) {
|
||||
|
|
@ -164,8 +162,7 @@ Ox.localStorage = function(namespace) {
|
|||
value = localStorage[namespace + '.' + key];
|
||||
ret = value === void 0 ? void 0 : JSON.parse(value);
|
||||
} else {
|
||||
args = Ox.makeObject(arguments);
|
||||
Ox.forEach(args, function(value, key) {
|
||||
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
|
||||
localStorage[namespace + '.' + key] = JSON.stringify(value);
|
||||
});
|
||||
ret = this;
|
||||
|
|
@ -173,11 +170,14 @@ Ox.localStorage = function(namespace) {
|
|||
return ret;
|
||||
};
|
||||
// IE 8 doesn't like `storage.delete`
|
||||
storage['delete'] = function(key) {
|
||||
var keys = arguments.length == 0 ? Object.keys(storage()) : [key];
|
||||
storage['delete'] = function() {
|
||||
var keys = arguments.length == 0 ? Object.keys(storage())
|
||||
: Ox.toArray(arguments)
|
||||
keys.forEach(function(key) {
|
||||
delete localStorage[namespace + '.' + key];
|
||||
});
|
||||
var keys =
|
||||
keys
|
||||
return storage;
|
||||
};
|
||||
return storage;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue