forked from FormidableLabs/react-flux-concepts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
006.1-flux-form.html
133 lines (116 loc) · 3.91 KB
/
006.1-flux-form.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
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
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/0.11.6/react-router.js"></script>
<script src="https://kenwheeler.github.io/mcfly/McFly.js"></script>
</head>
<body>
<div id="app-container"></div>
<script type="text/jsx">
/** McFly */
var Flux = new McFly();
/** Store */
_recipes = [];
function addRecipes(recipe){
_recipes.push(recipe);
}
function clearRecipes(){
_recipes = [];
}
function deleteRecipe(index){
_recipes.splice(index,1);
}
var RecipeStore = Flux.createStore({
getRecipes: function(){
return _recipes;
}
}, function(payload){
if(payload.actionType === "ADD_RECIPE") {
addRecipes(payload.text);
RecipeStore.emitChange();
}
if(payload.actionType === "CLEAR_RECIPES") {
clearRecipes();
RecipeStore.emitChange();
}
if(payload.actionType === "DELETE_RECIPE") {
deleteRecipe(payload.index);
RecipeStore.emitChange();
}
});
/** Actions */
var RecipeActions = Flux.createActions({
addRecipe: function(text){
return {
actionType: "ADD_RECIPE",
text: text
}
},
clearRecipes: function(){
return {
actionType: "CLEAR_RECIPES"
}
},
deleteRecipe: function(index){
return {
actionType: "DELETE_RECIPE",
index: index
}
}
});
function getRecipes(){
return {
recipes: RecipeStore.getRecipes()
}
}
/** Controller View */
var RecipeBook = React.createClass({
mixins: [RecipeStore.mixin],
getInitialState: function(){
return getRecipes();
},
storeDidChange: function() {
console.log('changed')
this.setState(getRecipes());
},
render: function() {
return <Recipes recipes={this.state.recipes} />;
}
});
/** Component */
var Recipes = React.createClass({
addRecipe: function(){
RecipeActions.addRecipe(this.refs.inv.getDOMNode().value);
this.refs.inv.getDOMNode().value = "";
},
clearRecipes: function(){
RecipeActions.clearRecipes();
},
deleteRecipe: function(index){
RecipeActions.deleteRecipe(index);
},
render: function() {
var self = this;
return (
<div className="todos_app">
<input type="text" ref="inv"/>
<ul className="recipes">
{ this.props.recipes.map(function(recipe, index){
return <li key={index}>Recipe {recipe} <button type="button" onClick={self.deleteRecipe.bind(self, index)}>Delete</button></li>
})}
</ul>
<button onClick={this.clearRecipes}>Clear</button>
<button onClick={this.addRecipe}>Add</button>
</div>
)
}
});
React.render(<RecipeBook />, document.body);
</script>
</body>
</html>