Created
February 9, 2015 22:41
-
-
Save amix/88cc073049c1aba5c6af to your computer and use it in GitHub Desktop.
Flux + React example
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
# Store | |
class CounterStore extends EventEmitter | |
constructor: -> | |
@count = 0 | |
@dispatchToken = @registerToDispatcher() | |
increaseValue: (delta) -> | |
@count += 1 | |
getCount: -> | |
return @count | |
registerToDispatcher: -> | |
CounterDispatcher.register((payload) => | |
switch payload.type | |
when ActionTypes.INCREASE_COUNT | |
@increaseValue(payload.delta) | |
) | |
# Action | |
class CounterActions | |
@increaseCount: (delta) -> | |
CounterDispatcher.handleViewAction({ | |
'type': ActionTypes.INCREASE_COUNT | |
'delta': delta | |
}) | |
# View | |
CounterButton = React.createClass( | |
getInitialState: -> | |
return {'count': 0} | |
_onChange: -> | |
@setState({ | |
count: CounterStore.getCount() | |
}) | |
componentDidMount: -> | |
CounterStore.addListener('CHANGE', @_onChange) | |
componentWillUnmount: -> | |
CounterStore.removeListener('CHANGE', @_onChange) | |
render: -> | |
return React.DOM.button({'className': @prop.class}, @state.value) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment