0.0.3-alpha.7 • Published 9 years ago

kefaise-flux v0.0.3-alpha.7

Weekly downloads
10
License
ISC
Repository
github
Last release
9 years ago

Lightweight yet flexible implementation of flux pattern

Usage

Store as a handler function

var flux = require("kefaise-flux");

var eventName = "event"; //this can be Symbol in future

function simpleStore(type, data, emitChanges) {
    if(type === eventName) {
        this.data = data;
        emitChanges();
    }
}

flux.getDispatcher().registerStore("simpleStore", simpleStore);
flux.getDispatcher().subscribeToStore("simpleStore", handler);

flux.getDispatcher().dispatch(eventName, {data: "testData"});

function simpleStoreListener(store) {
    console.log(store.data); //should print {data: "testData"}
}

Store as an object

var flux = require("kefaise-flux");

var eventName = "event"; //this can be Symbol in future

function SimpleStore() {
    this.data = {};
}
SimpleStore.prototype.dispatch(type, data, emitChanges) {
    if(type === eventType) {
        this.data = data;
        emitChanges();
    }
}
SimpleStore.prototype.getData() {
    return this.data;
}

flux.getDispatcher().registerStore("simpleStore", new SimpleStore());
flux.getDispatcher().subscribeToStore("simpleStore", handler);

flux.getDispatcher().dispatch(eventName, {data: "testData"});


function simpleStoreListener(store) {
    console.log(store.getData()); ///should print {data: "testData"}
}