Skip to content

Instantly share code, notes, and snippets.

@Daniel15
Created May 10, 2015 07:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniel15/ee4342940b5562817789 to your computer and use it in GitHub Desktop.
Save Daniel15/ee4342940b5562817789 to your computer and use it in GitHub Desktop.
Simple Flux
import EventEmitter from 'events';
import Dispatcher from '../dispatcher';
const CHANGE_EVENT = 'change';
export default class BaseStore extends EventEmitter {
constructor() {
super();
this.dispatchToken = Dispatcher.register(this.handleDispatch.bind(this));
}
handleDispatch(payload) {
throw new Error(this.constructor.name + ' does not implement handleDispatch');
}
emitChange() {
this.emit(CHANGE_EVENT);
}
addChangeListener(callback) {
this.on(CHANGE_EVENT, callback);
}
removeChangeListener(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
}
import {Dispatcher} from 'flux';
export default new Dispatcher();
import {ActionTypes} from '../constants';
import BaseStore from './BaseStore';
class ExampleStore extends BaseStore {
handleDispatch(payload) {
switch (payload.type) {
case ActionTypes.INIT:
this._doStuff();
this.emitChange();
break;
}
}
getStuff() {
return 'Put some data here';
}
}
export default new ExampleStore();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment