ekm-flux-store v0.0.8
ekm-flux-store
A generic Flux Store for javascript applications using the flux application architecture.
Example
import FluxStore from 'ekm-flux-store';
import AppDispatcher from '../Dispatchers/AppDispatcher';
import Constants from '../Constants/MyConstants';
const initialState = { ... };
class MyStore extends FluxStore {
// Call constructor with initial state and the change event constant
constructor() {
super({ ...initialState }, Constants.CHANGE_EVENT);
}
// The function that is called when an event is dispatched
reduce = (action, changeState) => {
switch (action.actionType) {
case Constants.GET_SUCCESS:
changeState( { ... } );
break;
default: break;
}
}
}
const Store = new MyStore();
AppDispatcher.register(Store.getNextState.bind(Store));
export default Store;
Sub Class Functionality
The sub class needs to initialise the base class by calling super
with the initialState
of the Store and the CHANGE_EVENT
. A function named reduce
also needs to be implemented into the sub class. This function will supply the action
that is dispatched and a changeState
callback function to the reduce
sub class function. The implementation of these functions can be seen in the example above.
Base Class Functionality
Properties
Current State
this.CurrentState;
Initial State
this.InitialState;
Functions
emitChange
Emits a change that will notify any listeners that a CHANGE_EVENT has occurred
Store.emitChange();
addChangeListener
Adds a callback function that will execute when a CHANGE_EVENT occurs
Store.addChangeListener(this.onChange);
removeChangeListener
Removes a previously added callback function from the store
Store.removeListener(this.onChange);
getInitialState
Returns the initial state of the store
Store.getInitialState();
clearState
Resets the store to the initial state of the store
Store.clearState();
getState
Returns the current state of the store
Store.getState();