Created
May 10, 2015 07:40
-
-
Save Daniel15/ee4342940b5562817789 to your computer and use it in GitHub Desktop.
Simple Flux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Dispatcher} from 'flux'; | |
export default new Dispatcher(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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