-
Notifications
You must be signed in to change notification settings - Fork 10
/
route-aware-components-jsx.html
80 lines (65 loc) · 1.91 KB
/
route-aware-components-jsx.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
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript" src="jquery-2.0.3/build/jquery.js"></script>
<script type="text/javascript" src="underscore-1.5.2/build/underscore.js"></script>
<script type="text/javascript" src="backbone-1.1.0/build/backbone.js"></script>
<script type="text/javascript" src="react-0.8.0/build/react.js"></script>
<script type="text/javascript" src="react-0.8.0/build/JSXTransformer.js"></script>
</head>
<body>
<script type="text/jsx">
/**
* @jsx React.DOM
*/
var FooComponent = React.createClass({
render : function() {
return <div>foo</div>;
}
});
var BarComponent = React.createClass({
render : function() {
return <div>bar</div>;
}
});
var InterfaceComponent = React.createClass({
componentWillMount : function() {
this.callback = (function() {
this.forceUpdate();
}).bind(this);
this.props.router.on("route", this.callback);
},
componentWillUnmount : function() {
this.props.router.off("route", this.callback);
},
render : function() {
if (this.props.router.current == "foo") {
return <FooComponent />;
}
if (this.props.router.current == "bar") {
return <BarComponent />;
}
return <div />;
}
});
var Router = Backbone.Router.extend({
routes : {
"foo" : "foo",
"bar" : "bar"
},
foo : function() {
this.current = "foo";
},
bar : function() {
this.current = "bar";
}
});
var router = new Router();
React.renderComponent(
<InterfaceComponent router={router} />,
document.body
);
Backbone.history.start();
</script>
</body>
</html>