Fluxxor

fluxxor

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var React = require("react"),
Fluxxor = require("../../../");
window.React = React;
var constants = {
ADD_TODO: "ADD_TODO",
TOGGLE_TODO: "TOGGLE_TODO",
CLEAR_TODOS: "CLEAR_TODOS"
};
var TodoStore = Fluxxor.createStore({
initialize: function() {
this.todoId = 0;
this.todos = {};
this.bindActions(
constants.ADD_TODO, this.onAddTodo,
constants.TOGGLE_TODO, this.onToggleTodo,
constants.CLEAR_TODOS, this.onClearTodos
);
},
onAddTodo: function(payload) {
debugger;

var id = this._nextTodoId();
var todo = {
id: id,
text: payload.text,
complete: false
};
console.log('TodoStore');
this.todos[id] = todo;
this.emit("change");
},
onToggleTodo: function(payload) {

var id = payload.id;
this.todos[id].complete = !this.todos[id].complete;
this.emit("change");

},
onClearTodos: function() {
var todos = this.todos;
Object.keys(todos).forEach(function(key) {
if(todos[key].complete) {
delete todos[key];
}
});
this.emit("change");
},
getState: function() {
return {
todos: this.todos
};
},
_nextTodoId: function() {
return ++this.todoId;
}
});
var actions = {
addTodo: function(text) {
console.log('action');
this.dispatch(constants.ADD_TODO, {text: text});
},
toggleTodo: function(id) {
this.dispatch(constants.TOGGLE_TODO, {id: id});
},
clearTodos: function() {
this.dispatch(constants.CLEAR_TODOS);
}
};
var stores = {
TodoStore: new TodoStore()
};
var flux = new Fluxxor.Flux(stores, actions);
window.flux = flux;
flux.on("dispatch", function(type, payload) {
debugger;

if (console && console.log) {
console.log("[Dispatch]", type, payload);
}
});
var FluxMixin = Fluxxor.FluxMixin(React),
StoreWatchMixin = Fluxxor.StoreWatchMixin;
var Application = React.createClass({
mixins: [FluxMixin, StoreWatchMixin("TodoStore")],
getInitialState: function() {
return { newTodoText: "" };
},
getStateFromFlux: function() {
var flux = this.getFlux();
// Our entire state is made up of the TodoStore data. In a larger
// application, you will likely return data from multiple stores, e.g.:
// return {
// todoData: flux.store("TodoStore").getState(),
// userData: flux.store("UserStore").getData(),
// fooBarData: flux.store("FooBarStore").someMoreData()
// };
return flux.store("TodoStore").getState();
},
render: function() {
var todos = this.state.todos;
return (
<div>
<ul>
{Object.keys(todos).map(function(id) {
return <li key={id}><TodoItem todo={todos[id]} /></li>;
})}
</ul>
<form onSubmit={this.onSubmitForm}>
<input type="text" size="30" placeholder="New Todo"
value={this.state.newTodoText}
onChange={this.handleTodoTextChange} />
<input type="submit" value="Add Todo" />
</form>
<button onClick={this.clearCompletedTodos}>Clear Completed</button>
</div>
);
},
handleTodoTextChange: function(e) {
this.setState({newTodoText: e.target.value});
},
onSubmitForm: function(e) {
e.preventDefault();
if (this.state.newTodoText.trim()) {
this.getFlux().actions.addTodo(this.state.newTodoText);
this.setState({newTodoText: ""});
}
},
clearCompletedTodos: function(e) {
this.getFlux().actions.clearTodos();
}
});
var TodoItem = React.createClass({
mixins: [FluxMixin],
propTypes: {
todo: React.PropTypes.object.isRequired
},
render: function() {
var style = {
textDecoration: this.props.todo.complete ? "line-through" : ""
};
return <span style={style} onClick={this.onClick}>{this.props.todo.text}</span>;
},
onClick: function() {
this.getFlux().actions.toggleTodo(this.props.todo.id);
}
});
React.render(<Application flux={flux} />, document.getElementById("app"));