js-store
v0.0.11
Published
simple store creation utils
Downloads
9
Readme
js-store
utilities for simple javascript stores
install
npm install --save js-store
usage
Simple Store
var jsStore = require('js-store');
var myStore = jsStore.createStore({foo: 'bar'});
myStore.setState({baz: 'qux'});
myStore.getState(); // {foo: 'bar', baz: 'qux'});
var cb = function() {
// pull data from store using getState
}
myStore.addChangeListener(cb);
myStore.setState({other: 'value'}); // cb will fire
myStore.removeChangeListener(cb);
myStore.setState({other: 'another value'}); // cb will not fire
Store Collections
var jsStore = require('js-store');
var TeamsStore = jsStore.createCollectionStore('teams');
TeamsStore.getState(); // returns {teams: []}
TeamsStore.setState({teams: [
{id: 1, name: 'a'},
{id: 2, name: 'b'},
{id: 3, name: 'c'}
]});
TeamsStore.findById(2); // returns {id: 2, name: 'b'}
Store collections have a few helper methods to make it easier to work with the collection:
// returns the object with an id of 2, or undefined
// pass an optional second arg with a true to convert the first arg to an integer)
TeamStore.findById(2)
// where obj.id is a unique identifier, or obj is the value of an id
// returns the index of the object, or -1 if not found
TeamStore.findIndex(obj)
// adds an item to the collection
TeamStore.add(item)
// replaces existing item (by team.id)
// returns the updated item or undefined if the item.id doesn't match
TeamStore.replace(item)
// add or replaces existing item (by team.id)
// returns the new item
TeamStore.upsert(item)
// replace the existing item that has the given id
// returns the id if successfully destroy, or undefined
TeamStore.destory(id)
Stores that sync with localStorage
var jsStore = require('js-store');
var TeamsStore = jsStore.createCollectionStore('teams', true);
// changes to TeamsStore's state will now write to localStorage
// TeamStore's state will also start with state from localstorage
Syncing with local storage is also available for simple stores. To do so pass along a string key to the createStore
method.
var jsStore = require('js-store');
var myStore = jsStore.createStore({}, 'myStore');
To make singleton stores for your client side app, simply export a creaetd store from a module
var jsStore = require('js-store');
var UserStore = jsStore.createStore({}, 'user');
module.exports = UserStore;