-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample1.js
120 lines (107 loc) · 2.38 KB
/
example1.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var redux = require('../')
var todo = function(state,action) {
switch (action.type) {
case 'ADD_TODO' :
return {
id: action.id,
text: action.text,
completed:false
}
break;
case 'TOGGLE_TODO' :
if (state.id !== action.id) {
return state
}
return {
type: state.type,
id : state.id,
text: state.text,
completed: !state.completed
}
break;
default:
return state
break;
}
}
var todos = function(state, action) {
if (state === undefined)
state = []
switch (action.type) {
case 'ADD_TODO':
return state.concat(todo(undefined,action))
break;
case 'TOGGLE_TODO' :
return state.map(function(t) {
return todo(t,action)
})
break;
default:
return state;
}
}
var visibilityFilter = function(state, action) {
if (state === undefined)
state = 'SHOW_ALL'
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
break;
default:
return state
}
}
var combineReducers = redux.combineReducers;
/*
var todoApp = function(state, action) {
if (state === undefined)
state = {}
return {
todos: todos(state.todos,action),
visibilityFilter: visibilityFilter(state.visibilityFilter,action)
}
}
*/
var todoApp = combineReducers({
todos:todos,
visibilityFilter:visibilityFilter
});
var createStore = redux.createStore;
var store = createStore(todoApp)
console.log('Initial state:');
console.log(store.getState());
console.log('---------------')
console.log('Dispatching ADD_TODO');
store.dispatch({
type:'ADD_TODO',
id: 0,
text:'Learn Redux'
});
console.log('Current state:');
console.log(store.getState());
console.log('---------------')
console.log('Dispatching ADD_TODO');
store.dispatch({
type:'ADD_TODO',
id: 1,
text:'Go Shopping'
});
console.log('Current state:');
console.log(store.getState());
console.log('---------------')
console.log('Dispatching TOGGLE_TODO');
store.dispatch({
type:'TOGGLE_TODO',
id: 0
});
console.log('Current state:');
console.log(store.getState());
console.log('---------------')
console.log('Dispatching SET_VISIBILITY_FILTER')
store.dispatch({
type:'SET_VISIBILITY_FILTER',
filter:'SHOW_COMPLETED'
});
console.log('Current state:');
console.log(store.getState());
console.log('---------------')