kefaise-flux
v0.0.3-alpha.7
Published
Lightweight yet flexible implementation of flux pattern
Downloads
5
Readme
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"}
}