1 |
|
2 |
|
3 |
|
4 |
|
5 | var React = process.env.NODE_ENV === 'production' ? require('react') : require('react/addons');
|
6 | var Router = require('react-router');
|
7 | var el = React.createElement;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | exports['default'] = PoeApp;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function PoeApp(element, context, cb) {
|
23 | cb = cb || function() {};
|
24 | var isString = typeof element === 'string';
|
25 |
|
26 | var router = Router.create({
|
27 | location: isString ? element : Router.HistoryLocation,
|
28 | routes: context.routes(el, $get, context)
|
29 | });
|
30 |
|
31 | if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
|
32 | window.hyperFormat = context.format;
|
33 | window.hyperStore = context.store;
|
34 | }
|
35 |
|
36 | router.run(function(Handler) {
|
37 | React.withContext(context, function() {
|
38 | var root = el(Handler);
|
39 |
|
40 | if (isString) return cb(null, React.renderToStaticMarkup.bind(null, root));
|
41 | return React.render(root, element);
|
42 | });
|
43 | });
|
44 |
|
45 | return router;
|
46 | }
|
47 |
|
48 | exports.React = React;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
|
55 | window.React = React;
|
56 | }
|
57 |
|
58 | function $get(path, parent, fallback) {
|
59 | for (var i = 0, child; i < path.length; i++) {
|
60 | if (!parent) return undefined;
|
61 | child = parent[path[i]];
|
62 | if (typeof child === 'function') parent = child.bind(parent);
|
63 | else parent = child;
|
64 | }
|
65 | return parent;
|
66 | }
|