UNPKG

1.46 kBJavaScriptView Raw
1/**
2 * Module dependencies
3 */
4
5var React = process.env.NODE_ENV === 'production' ? require('react') : require('react/addons');
6var Router = require('react-router');
7var el = React.createElement;
8
9/**
10 * Expose the PoeApp
11 */
12
13exports['default'] = PoeApp;
14
15/**
16 * Create a PoeApp
17 *
18 * @param {Object} routes
19 * @param {String} name
20 */
21
22function 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
48exports.React = React;
49
50/**
51 * Expose React to the window
52 */
53
54if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
55 window.React = React;
56}
57
58function $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}