Skip to content
This repository was archived by the owner on Dec 19, 2017. It is now read-only.

Commit d28b2a8

Browse files
authoredJan 11, 2017
Add Router.setConfig and Router.useRoutes (#147)
* Update yarn.lock * Add Router.setConfig and Router.useRoutes * Add docs
1 parent ab041fc commit d28b2a8

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed
 

‎docs/router.md

+6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ Top-most router
2727
#### Router.tail
2828
Deepest router
2929

30+
#### Router.setConfig({ base = '', hashbang = false })
31+
Sets router configuration
32+
3033
#### Router.use(fn)
3134
Convenience function for `Router.middleware.push(fn)`
3235

3336
#### Router.usePlugin(fn)
3437
Convenience function for `Router.plugins.push(fn)`
3538

39+
#### Router.useRoutes(routes)
40+
Convenience function for `Object.assign(Router.routes, routes)`
41+
3642
#### Router.update(path, [push = true], [options = { push: true, force: false, with: {} }])
3743
Convenience function for `Router.get(0).update(...)`
3844

‎src/route.js

+4
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,8 @@ export default class Route {
114114

115115
await routeBeforeRender
116116
}
117+
118+
static createRoutes(routes) {
119+
return Object.entries(routes).map(([r, m]) => new Route(r, m))
120+
}
117121
}

‎src/router.js

+43-35
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,40 @@ class Router {
1414
constructor(params) {
1515
this.component = ko.observable()
1616
this.isNavigating = ko.observable(true)
17+
this.routes = Route.createRoutes(params.routes || {})
1718

18-
Router.link(this, params)
19-
20-
this.passthrough = params
19+
Router.link(this)
2120

2221
if (this.isRoot) {
22+
Router.setConfig(params)
23+
this.routes.push(...Route.createRoutes(Router.routes))
2324
document.addEventListener(events.click, Router.onclick)
2425
window.addEventListener(events.popstate, Router.onpopstate)
25-
}
26-
27-
const routes = Object.assign(this.isRoot ? Router.routes : {}, params.routes)
28-
this.routes = Object.entries(routes).map(([r, m]) => new Route(r, m))
29-
if (!this.isRoot && this.$parent.ctx.route.children) {
26+
} else if (this.$parent.ctx.route.children) {
3027
this.routes.push(...this.$parent.ctx.route.children)
3128
}
32-
delete params.routes
29+
30+
this.passthrough = Object.entries(params).reduce((accum, [k, v]) =>
31+
k === 'base' || k === 'hashbang' || k === 'routes'
32+
? accum
33+
: Object.assign(accum, { [k]: v }),
34+
{})
3335

3436
this.update(this.getPathFromLocation(), false)
3537
}
3638

37-
static async update(...args) {
38-
return await routers[0].update(...args)
39+
get base() {
40+
return this.isRoot
41+
? (Router.config.hashbang ? '/#!' : '') + Router.config.base
42+
: this.$parent.base + this.$parent.ctx.pathname
43+
}
44+
45+
get $parent() {
46+
return routers[this.depth - 1]
47+
}
48+
49+
get $child() {
50+
return routers[this.depth + 1]
3951
}
4052

4153
async update(url, args) {
@@ -152,6 +164,15 @@ class Router {
152164
}
153165
}
154166

167+
static setConfig({ base, hashbang }) {
168+
if (base) {
169+
Router.config.base = base
170+
}
171+
if (hashbang) {
172+
Router.config.hashbang = hashbang
173+
}
174+
}
175+
155176
static use(...fns) {
156177
Router.middleware.push(...fns)
157178
}
@@ -160,6 +181,10 @@ class Router {
160181
Router.plugins.push(...fns)
161182
}
162183

184+
static useRoutes(routes) {
185+
Object.assign(Router.routes, routes)
186+
}
187+
163188
static get(i) {
164189
return routers[i]
165190
}
@@ -172,32 +197,15 @@ class Router {
172197
return routers[routers.length - 1]
173198
}
174199

175-
static link(router, params) {
176-
routers.push(router)
177-
router.depth = routers.length - 1
200+
static async update(...args) {
201+
return await routers[0].update(...args)
202+
}
203+
204+
static link(router) {
205+
router.depth = routers.length
178206
router.isRoot = router.depth === 0
207+
routers.push(router)
179208
router.$root = routers[0]
180-
181-
if (router.isRoot) {
182-
if (params.base) {
183-
Router.config.base = params.base
184-
delete params.base
185-
}
186-
router.base = Router.config.base
187-
if (params.hashbang) {
188-
Router.config.hashbang = params.hashbang
189-
delete params.hashbang
190-
}
191-
if (Router.config.hashbang) {
192-
router.base += '/#!'
193-
}
194-
} else {
195-
const $parent = routers[router.depth - 1]
196-
const { ctx: { pathname }, base } = $parent
197-
router.$parent = $parent
198-
router.$parent.$child = router
199-
router.base = base + pathname
200-
}
201209
}
202210

203211
static unlink() {

‎test/routing/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ ko.components.register('routing', {
3939
r
4040
])
4141

42-
Router.routes = mapValues({
42+
Router.useRoutes(mapValues({
4343
..._static
4444
}, (r) => [
4545
(ctx) => extend(ctx, { t, next }),
4646
r
47-
])
47+
]))
4848

4949
next()
5050
}

0 commit comments

Comments
 (0)