-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathapp.js
74 lines (64 loc) · 1.7 KB
/
app.js
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
var React = require('react');
var Router = require('react-router');
var { Route, RouteHandler, Link } = Router;
var AsyncElement = {
loadedComponent: null,
load: function () {
if (this.constructor.loadedComponent)
return;
this.bundle(function (component) {
this.constructor.loadedComponent = component;
this.forceUpdate();
}.bind(this));
},
componentDidMount: function () {
setTimeout(this.load, 1000); // feel it good
},
render: function () {
var Component = this.constructor.loadedComponent;
if (Component) {
// can't find RouteHandler in the loaded component, so we just grab
// it here first.
this.props.activeRoute = <RouteHandler/>;
return <Component {...this.props}/>;
}
return this.preRender();
}
};
var PreDashboard = React.createClass({
mixins: [ AsyncElement ],
bundle: require('bundle?lazy!./dashboard.js'),
preRender: function () {
return <div>Loading dashboard...</div>;
}
});
var PreInbox = React.createClass({
mixins: [ AsyncElement ],
bundle: require('bundle?lazy!./inbox.js'),
preRender: function () {
return <div>Loading inbox...</div>;
}
});
var App = React.createClass({
render: function () {
return (
<div>
<h1>Partial App</h1>
<ul>
<li><Link to="dashboard">Dashboard</Link></li>
</ul>
<RouteHandler/>
</div>
);
}
});
var routes = (
<Route handler={App}>
<Route name="dashboard" path="dashboard" handler={PreDashboard}>
<Route name="inbox" path="dashboard/inbox" handler={PreInbox}/>
</Route>
</Route>
);
Router.run(routes, function (Handler) {
React.render(<Handler/>, document.getElementById('example'));
});