diff --git a/examples/documentation/js/example.js b/examples/documentation/js/example.js index bbc8d6c6..e20d55f0 100644 --- a/examples/documentation/js/example.js +++ b/examples/documentation/js/example.js @@ -97,13 +97,54 @@ My.range = function() { return a; }; - -My.foo = { - array: [ - {a: 1, b: 2}, - {c: 3, d: 4} - ], - objects: { - +/*@ +My.localStorage Returns a localStorage handler for a given namespace + (ns) -> storage localStorage handler + () -> Returns all key:value pairs + (key) -> <*> Returns one value + (key, value) -> Sets one value, returns the handler + ({key: value, ...}) -> Sets one or more values, returns the handler + key Any string + value <*> Any value that can be JSON-serialized + .delete Delete method + () -> Deletes all key:value pairs, returns the handler + (key[, ...]) -> Deletes one or more pairs, returns the handler + key Any string + ns Namespace +*/ +My.localStorage = (function() { + if (!window.localStorage) { + window.localStorage = {}; } -} \ No newline at end of file + return function(ns) { + function storage(key, value) { + var args, ret; + if (arguments.length == 0) { + ret = {}; + Ox.forEach(localStorage, function(value, key) { + if (Ox.startsWith(key, ns + '.')) { + ret[key.slice(ns.length + 1)] = JSON.parse(value); + } + }); + } else if (arguments.length == 1 && typeof key == 'string') { + value = localStorage[ns + '.' + key]; + ret = Ox.isUndefined(value) ? void 0 : JSON.parse(value); + } else { + Ox.forEach(Ox.makeObject(arguments), function(value, key) { + localStorage[ns + '.' + key] = JSON.stringify(value); + }); + ret = this; + } + return ret; + }; + storage.delete = function() { + var keys = arguments.length == 0 ? Object.keys(storage()) + : Ox.toArray(arguments) + keys.forEach(function(key) { + delete localStorage[ns + '.' + key]; + }); + return storage; + }; + return storage; + }; +}(); \ No newline at end of file diff --git a/source/Ox/js/Core.js b/source/Ox/js/Core.js index a35be326..1e40d05e 100644 --- a/source/Ox/js/Core.js +++ b/source/Ox/js/Core.js @@ -83,7 +83,7 @@ Some conventions: @*/ global.Ox = function(value) { return Ox.wrap(value); - }; + }; })(this); /*@ @@ -139,8 +139,6 @@ Ox.load = function() { /*@ Ox.localStorage localStorage wrapper (namespace) -> storage localStorage function for a given namespace - FIXME: there is a bug in Ox.doc here, - will use "(namespace)" as function name () -> returns all key:value pairs (key) -> <*> returns one value (key, val) -> 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; diff --git a/source/Ox/js/Polyfill.js b/source/Ox/js/Polyfill.js index 1986f49c..2e2bb491 100644 --- a/source/Ox/js/Polyfill.js +++ b/source/Ox/js/Polyfill.js @@ -2,7 +2,7 @@ Ox.polyfill = {}; -(function(global) { +(function(window) { /*@ Ox.polyfill.bind see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind