-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathredux-example.html
66 lines (57 loc) · 1.97 KB
/
redux-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html>
<head>
<title>Redux example</title>
<meta charset="utf-8">
</head>
<body>
<!-- target element -->
<my-counter></my-counter>
<!-- our templates -->
<template data-tagname="my-counter">
<p>
Clicked: <span id="value">{{ count }}</span> times
<button onclick="increment()">+</button>
<button onclick="decrement()">-</button>
</p>
</template>
<!-- dependencies -->
<script src="./redux.min.js"></script>
<script src="../build/magery-runtime.js"></script>
<script src="../build/magery-compiler.js"></script>
<!-- application code -->
<script>
var components = MageryCompiler.compile('template');
// create a store
var store = Redux.createStore(function (state, action) {
if (typeof state === 'undefined') {
return {count: 0};
}
switch (action.type) {
case 'INCREMENT':
return {count: state.count + 1};
case 'DECREMENT':
return {count: state.count - 1};
default:
return state;
}
});
var target = document.querySelector('my-counter');
var handlers = {};
function render() {
components['my-counter'](target, store.getState(), handlers);
}
// add event handlers using Magery
handlers.increment = function () {
store.dispatch({type: 'INCREMENT'});
};
handlers.decrement = function () {
store.dispatch({type: 'DECREMENT'});
};
// update the page when the store changes
store.subscribe(render);
// initial render
render();
</script>
</body>
</html>