allow namespaces to be turned off
This commit is contained in:
parent
3a2ada1ce0
commit
7b459f1f87
32
README.md
32
README.md
|
@ -25,17 +25,19 @@ Usage
|
|||
|
||||
Made for Node.js and Bower (browser-side).
|
||||
|
||||
var localStorage = require('localStorage')
|
||||
, JsonStorage = require('json-storage').JsonStorage
|
||||
, store = JsonStorage.create(localStorage, 'my-widget-namespace', { stringify: true })
|
||||
, myValue = {
|
||||
foo: "bar"
|
||||
, baz: "quux"
|
||||
}
|
||||
;
|
||||
```javascript
|
||||
var localStorage = require('localStorage')
|
||||
, JsonStorage = require('json-storage').JsonStorage
|
||||
, store = JsonStorage.create(localStorage, 'my-widget-namespace', { stringify: true })
|
||||
, myValue = {
|
||||
foo: "bar"
|
||||
, baz: "quux"
|
||||
}
|
||||
;
|
||||
|
||||
store.set('myKey', myValue);
|
||||
myValue = store.get('myKey');
|
||||
store.set('myKey', myValue);
|
||||
myValue = store.get('myKey');
|
||||
```
|
||||
|
||||
NOTE: When using with Node and the `localStorage` module,
|
||||
you may wish to pass the `{ stringify: false }` option to prevent double stringification.
|
||||
|
@ -48,6 +50,8 @@ API
|
|||
* `namespace` is optional string which allows multiple non-conflicting storage containers. For example you could pass two widgets different storage containers and not worry about naming conflicts:
|
||||
* `Gizmos.create(JsonStorage.create(null, 'my-gizmos'))`
|
||||
* `Gadgets.create(JsonStorage.create(null, 'my-gadgets'))`
|
||||
* Namespacing can be turned off by explicitly setting `false`
|
||||
* `Gadgets.create(JsonStorage.create(null, false))`
|
||||
* `opts`
|
||||
* `stringify` set to `false` in `node` to avoid double stringifying
|
||||
* `store.get(key)`
|
||||
|
@ -82,9 +86,11 @@ To save `undefined`, use `null` instead.
|
|||
|
||||
Note that both values that exist as `null` and values that don't exist at all will return `null`.
|
||||
|
||||
store.set('existing-key', null);
|
||||
null === store.get('existing-key');
|
||||
null === store.get('non-existant-key');
|
||||
```javascript
|
||||
store.set('existing-key', null);
|
||||
null === store.get('existing-key');
|
||||
null === store.get('non-existant-key');
|
||||
```
|
||||
|
||||
|
||||
### `null` vs `"null"`
|
||||
|
|
|
@ -52,6 +52,9 @@
|
|||
// complicated to figure it out
|
||||
this._namespace = delim;
|
||||
this._namespace += (namespace || 'jss');
|
||||
if (false === this._namespace) {
|
||||
this._namespace = '';
|
||||
}
|
||||
|
||||
this._store = w3cStorage;
|
||||
this._keysAreDirty = true;
|
||||
|
@ -107,7 +110,7 @@
|
|||
|
||||
delimAt = key.lastIndexOf(this._namespace);
|
||||
// test if this key belongs to this widget
|
||||
if (-1 !== delimAt) {
|
||||
if (!this._namespace || (-1 !== delimAt)) {
|
||||
this._keys.push(key.substr(0, delimAt));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"name": "json-storage",
|
||||
"description": "A wrapper for storage engines which use the W3C Storage API",
|
||||
"keywords": ["dom", "storage", "json", "w3c", "localStorage", "sessionStorage", "globalStorage", "Storage"],
|
||||
"version": "2.0.1",
|
||||
"version": "2.1.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/coolaj86/json-storage-js.git"
|
||||
|
|
Loading…
Reference in New Issue