This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
006-flux.html
108 lines (90 loc) · 3.16 KB
/
006-flux.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
<!-- https://docs.google.com/drawings/d/18EpRSCM3yicxFrF0CVODMs-uQUEfRDstXPtEZnWlqOc/edit?usp=sharing -->
<!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">
/**
*
* Assignment:
*
* 1. Add a new button that has a new callback that fires a new action
* 2. Update the store with new data (ie., "Kittens" + Math.floor(Math.random()*100000))
* 3. console.log(getRecipes()) the new store value from a component's render method to make sure the data made the trip
*
*/
/** McFly */
var Flux = new McFly();
/** Store */
_recipes = [];
function addRecipe(text){
_recipes.push(text);
}
var RecipeStore = Flux.createStore({
getRecipes: function(){
return _recipes;
}
}, function(payload){
if(payload.actionType === "ADD_RECIPE") {
addRecipe(payload.text);
RecipeStore.emitChange();
}
});
/** Actions */
var RecipeActions = Flux.createActions({
addRecipe: function(text){
return {
actionType: "ADD_RECIPE",
text: text
}
}
});
function getRecipes(){
return {
recipes: RecipeStore.getRecipes()
}
}
/** Controller View */
var RecipesController = React.createClass({
mixins: [RecipeStore.mixin],
getInitialState: function(){
return getRecipes();
},
storeDidChange: function() {
this.setState(getRecipes());
},
render: function() {
return <Recipes recipes={this.state.recipes} />;
}
});
/** Component */
var Recipes = React.createClass({
addRecipe: function(){
RecipeActions.addRecipe({_id: Math.floor(Math.random()*1000000)});
},
render: function() {
return (
<div className="recipes_app">
<ul className="recipes">
{ this.props.recipes.map(function(recipe, index){
return <li key={index}>recipe {index} : {recipe._id}</li>
})}
</ul>
<button onClick={this.addRecipe}>Add recipe</button>
</div>
)
}
});
React.render(<RecipesController />, document.body);
</script>
</body>
</html>