Created
July 29, 2016 01:34
-
-
Save tmcw/f103f82ebf79ac4474a943d29a1f2f1f to your computer and use it in GitHub Desktop.
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
<html> | |
<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></script> | |
<h1 class='js-target-count'></h1> | |
<button data-action='{"type":"increment"}'>+</button> | |
<button data-action='{"type":"decrement"}'>-</button> | |
<script> | |
var state = { | |
count: 0 | |
}; | |
function reducer(state, action) { | |
switch (action.type) { | |
case 'increment': state.count++; break; | |
case 'decrement': state.count--; break; | |
} | |
return state; | |
} | |
$('[data-action]').click(function(e) { | |
e.preventDefault(); | |
render(state = reducer(state, $(this).data('action'))); | |
}); | |
render(state); | |
function render(state) { | |
$('.js-target-count').text(state.count); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment