Table of Contents generated with DocToc
Observable store
An observable data store with dirty checking and computed properties.
Installation
npm install observable-store
How to use
Require observable-store:
var Store = ;
Initialization
It can be initialized with or without data. When initialized with data, a shallow clone is done first.
// Without data, an object based store:var store = {}; // Without data, an array base store:var store = ; // With an arrayvar store = "array" "of" "items"; // With an objectvar store = property1: "object" property2: "with" property3: "data";
Standard CRUD methods
store; // "array";store; // "object" store; // set or update the valuestore; // set or update the value store; // remove an item for the arraystore; // remove the property from the array // several items can be removed at once toostore;
Watch changes on specific property/item
The point of proxying accessing objects/arrays is that it can trigger events on what's changing. Examples of watching changes on specific properties or items:
var handle = store; store;
var handle = store; store;
var handle = store; store;
Watch changes on generic events such as added/updated/deleted
You can also watch generic events on the whole array/object to know when a new item or property is added, updated or deleted. The following examples are for an object-based store but work the same way with arrays.
var handle = store; store;
var handle = store; store;
var handle = store; store;
Unwatch changes
To unwatch, pass the handle
to unwatch
or unwatchValue
store; // for handles created by watchstore; // for handles created by watchValue
Using an array's native mutative methods
The data store has a method for accessing the array's native mutative methods to mutate the data store. When the changes have been made, the data store does a dirty check to figure out the changes and publish events accordingly. This example would also work with other methods than splice
like pop
, push
, shift
, unshift
, sort
, reverse
.
// Let's remove item 0 and 1 from this storevar store = 0 1 2 3; store; store; // For array-based storesstore; // returns [0, 1];
Using an array's accessor methods.
While alter
would work in these cases too, using proxy
instead doesn't trigger the dirty checking, so proxy
is as fast as the native method itself. Other accessor methods are: concat
, join
, slice
, toString
, toLocalString
, indexOf
, lastIndexOf
.
var store = 0 1 2 3; store; // returns "0|1|2|3";
Computed properties
The data store can also create computed properties out of other properties:
var store = "firstname": "John" "lastname": "Doe"; store; store; // "John Doe" store; // true store;
A computed property can also be watched upon:
// will be triggered if firstname or lastname changesstore; store;
Store reset
The store can be reused and its data reset. When calling reset, the store will do a diff with the previous data and publish the relevant events.
// Will set the internal data and publish events for everything that has been changed/updated/addedstore;
Store utilities
store; // will serialize the data store; // will return the internal structure store;
CHANGELOG
0.0.5 - 7 APR 2015
- Update to watch-notify 0.0.3
0.0.4 - 5 APR 2015
- Publish old value too when altering the store
LICENSE
MIT