forked from FormidableLabs/react-flux-concepts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path010-router.html
128 lines (123 loc) · 2.96 KB
/
010-router.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
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
</style>
<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>
<!-- ADDING REACT ROUTER -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/0.11.6/react-router.js"></script>
<script type="text/javascript">
var data = [
{
_id: "QQQ",
title: "Stuffed Chard",
instructions: "Stuff the chard...",
detailsRendered: "FOO CHARD DETAILS!"
},
{
_id: "YYY",
title: "Eggplant and Polenta",
instructions: "Put the eggplant in the oven...",
detailsRendered: "BAR EGGPLANT DETAILS!"
}
];
</script>
</head>
<body>
<div id="app-container"></div>
<script type="text/jsx">
var Recipe = React.createClass({
render: function() {
return (
<div>
<h2> {this.props.title} </h2>
<p> {this.props.instructions} </p>
</div>
);
}
});
var RecipeList = React.createClass({
/* GETTER */
getInitialState: function () {
return {data: data}
},
render: function() {
var recipeNodes = this.state.data.map(function(recipe, index){
return (
<Recipe
key={index}
title={recipe.title}
instructions={recipe.instructions} />
)
})
return (
<div>
RecipeList.
{recipeNodes}
<RouteHandler {...this.props}/>
</div>
);
}
});
var RecipeForm = React.createClass({
render: function() {
return (
<div >
RecipeForm.
</div>
);
}
});
/* add a home route */
var Home = React.createClass({
render: function () {
return (
<div>
Home rendered, welcome text, etc
</div>
)
}
})
var RecipeBook = React.createClass({
render: function() {
return (
<div>
<div>
<Link to="/">Home</Link>
<Link to="recipes"> Recipes </Link>
</div>
Hello, world! I am a RecipeBook.
<RouteHandler {...this.props}/>
</div>
);
}
});
var Router = ReactRouter;
var Route = Router.Route;
var DefaultRoute = Router.DefaultRoute;
var Link = Router.Link
var RouteHandler = Router.RouteHandler;
/**
* STEP 1: define routes & 'handlers' (components are handlers)
* see: https://github.com/rackt/react-router/blob/master/docs/guides/overview.md#with-react-router
*/
var routes = (
<Route name="RecipeBook" path="/" handler={RecipeBook}>
<DefaultRoute handler={Home}/>
<Route name="recipes" handler={RecipeList}/>
<Route name="recipe" handler={Recipe}/>
<Route name="create" handler={RecipeForm}/>
</Route>
);
/**
* STEP 2: call router.run, pass params into handler so they are available to components
* move React.render into router
*/
Router.run(routes, function (Handler, state) {
var params = state.params;
React.render(<Handler params={params}/>, document.body);
});
</script>
</body>
</html>