Skip to content

Instantly share code, notes, and snippets.

@kyldvs
Last active August 29, 2015 14:25
Show Gist options
  • Save kyldvs/9de4c5d020b46a2d45e7 to your computer and use it in GitHub Desktop.
Save kyldvs/9de4c5d020b46a2d45e7 to your computer and use it in GitHub Desktop.
Store with map
class BaseStore {
constructor(dispatcher) {
this._dispatcher = dispatcher;
this._actionMap = this.getActionHandlers();
this._dispatchToken = this._dispatcher.register(payload => {
this._actionMap[payload.type](payload);
});
}
}
class Store extends BaseStore {
getActionHandlers() {
return {
foo(payload) {
console.log('foo', payload.bar);
},
bar(payload) {
console.log('bar');
},
};
}
}
var MyDispatcher = new Dispatcher();
var MyStore = new FooStore(MyDispatcher);
MyDispatcher.dispatch({type: 'foo', bar: 100});
MyDispatcher.dispatch({type: 'bar'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment