-
Notifications
You must be signed in to change notification settings - Fork 46
/
router.js
35 lines (32 loc) · 1.04 KB
/
router.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
// This is a very basic router, shared between the server (in server.js) and
// browser (in App.js), with each route defining the URL to be matched and the
// main component to be rendered
exports.routes = {
list: {
url: '/',
component: require('./PostList'),
},
view: {
url: /^\/posts\/(\d+)$/,
component: require('./PostView'),
},
}
// A basic routing resolution function to go through each route and see if the
// given URL matches. If so we return the route key and data-fetching function
// the route's component has declared (if any)
exports.resolve = function(url) {
for (var key in exports.routes) {
var route = exports.routes[key]
var match = typeof route.url === 'string' ? url === route.url : url.match(route.url)
if (match) {
var params = Array.isArray(match) ? match.slice(1) : []
return {
key: key,
fetchData: function(cb) {
if (!route.component.fetchData) return cb()
return route.component.fetchData.apply(null, params.concat(cb))
},
}
}
}
}