Created
June 2, 2015 23:16
-
-
Save chris-ramon/934b7c348267700c1991 to your computer and use it in GitHub Desktop.
flux notes - simple flux implementation.
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
// simple flux implementation based on facebook/react examples | |
// 1. Component | |
class App extends Component { | |
foo() { | |
yStoreActions.create(payload); | |
} | |
componentDidMount() { | |
yStore.on(CHANGE_EVENT, this._onChange); | |
} | |
componentWillUnmount() { | |
yStore.removeListener(CHANGE_EVENT, this._onChange); | |
} | |
_onChange() { | |
this.setState(); | |
} | |
} | |
// 2. Actions | |
var actions = { | |
create: function(payload) { | |
AppDispatcher.dispatch({ | |
actionType: EntityConstants.CREATE, | |
payload: payload | |
}); | |
} | |
}; | |
// 3. Store | |
class YStore extends EventEmitter { | |
emitChange() { | |
this.emit(CHANGE_EVENT); | |
} | |
} | |
AppDispatcher.register(function(action) { | |
swithc(action.actionType) { | |
case EntityConstats.CREATE: | |
YStore.emitChange(); | |
break; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment