bjornua / coldstorage

Immutable dispatcher for the flux architecture
MIT License
4 stars 0 forks source link

coldstorage

NPM version build status Downloads

Immutable dispatcher for the flux architecture.

Features

Installing

Coldstorage is implemented using CommonJS. Currently coldstorage is distributed using npm. It can be installed by running

npm install coldstorage

in your project.

Coldstorage runs both in Node and the browser via browserify (or the like).

Example Usage

Creating actions

You create actions by passing an array of strings into coldstorage.createActions:

var actions = coldstorage.createActions(['login', 'logout']);

Creating the stores

var userStore = coldstorage.createStore('user');
userStore = userStore.on([actions.login], function (login) {
    var authed = login.get('username') === 'admin' && login.get('password') === '1234';
    return this.set('authed', authed);
});

userStore = userStore.on([actions.logout], function () {
    return this.set('authed', false);
});
var alertStore = coldstorage.createStore('alert');

alertStore = alertStore.on([userStore], function (user) {
    var authed = user.get('authed');
    if (authed === true) {
        return this.set('message', 'You were logged in');
    }
    return this.set('message', 'You were logged out');
});

Initializing the dispatcher

var dispatcher = coldstorage.fromStores([
    userStore,
    alertStore
]);

Dispatching actions

var dispatcher = dispatcher.dispatch(actions.login, {'username': 'admin', 'password': '1234'});
var userstore = dispatcher.stores.get('user');

// Prints true
console.log(userstore.get('authed'));
// Prints admin
console.log(userstore.get('username'));

Contribution

Requests and discussion are very welcomed in github issues

Use github pull requests to contribute patches.

License

coldstorage is MIT-licensed.