From 5d61c6d9f56f57caa818c7cb33f968a9a1ca37e8 Mon Sep 17 00:00:00 2001 From: Marc van der Meulen Date: Sat, 13 Aug 2016 03:00:30 +0200 Subject: [PATCH 1/3] Fix named routes matching for default child --- examples/named-routes/app.js | 9 ++++++++- src/create-matcher.js | 10 +++++++++- src/create-route-map.js | 5 +++++ test/e2e/specs/named-routes.js | 25 ++++++++++++++++++++++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/examples/named-routes/app.js b/examples/named-routes/app.js index 44ae179d8..e948be1dd 100644 --- a/examples/named-routes/app.js +++ b/examples/named-routes/app.js @@ -6,6 +6,8 @@ Vue.use(VueRouter) const Home = { template: '
This is Home
' } const Foo = { template: '
This is Foo
' } const Bar = { template: '
This is Bar
' } +const Parent = { template: '
This is Parent
This is Parent
' } +const Child = { template: '
This is Child
' } const router = new VueRouter({ mode: 'history', @@ -13,7 +15,10 @@ const router = new VueRouter({ routes: [ { path: '/', name: 'home', component: Home }, { path: '/foo', name: 'foo', component: Foo }, - { path: '/bar', name: 'bar', component: Bar } + { path: '/bar', name: 'bar', component: Bar }, + { path: '/parent', name: 'parent', component: Parent, + children: [{ path: '', name: 'child', component: Child }] + } ] }) @@ -27,6 +32,8 @@ new Vue({
  • home
  • foo
  • bar
  • +
  • parent
  • +
  • child
  • diff --git a/src/create-matcher.js b/src/create-matcher.js index 0b2569791..1725f2c0a 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -30,7 +30,7 @@ export function createMatcher (routes: Array): Matcher { const { name } = location if (name) { - const record = nameMap[name] + const record = findNamedRecord(nameMap,name) if (record) { location.path = fillParams(record.path, location.params, `named route "${name}"`) return _createRoute(record, location, redirectedFrom) @@ -204,3 +204,11 @@ function resolveRecordPath (path: string, record: RouteRecord): string { function getFullPath ({ path, query = {}, hash = '' }) { return (path || '/') + stringifyQuery(query) + hash } + +function findNamedRecord(nameMap: RouteMap, name: string): ?RouteRecord { + const defaultRecord = nameMap['_' + name] + if(defaultRecord) { + return defaultRecord + } + return nameMap[name] +} \ No newline at end of file diff --git a/src/create-route-map.js b/src/create-route-map.js index f3e2ef37d..fd6883c44 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -60,6 +60,11 @@ function addRouteRecord ( pathMap[record.path] = record if (name) nameMap[name] = record + + if (path == '' && parent && parent.name) { + record.path = normalizePath(record.path) + nameMap['_' + parent.name] = record + } } function normalizePath (path: string, parent?: RouteRecord): string { diff --git a/test/e2e/specs/named-routes.js b/test/e2e/specs/named-routes.js index d16d38fca..80222afec 100644 --- a/test/e2e/specs/named-routes.js +++ b/test/e2e/specs/named-routes.js @@ -3,11 +3,13 @@ module.exports = { browser .url('http://localhost:8080/named-routes/') .waitForElementVisible('#app', 1000) - .assert.count('li a', 3) + .assert.count('li a', 5) // assert correct href with base .assert.attributeContains('li:nth-child(1) a', 'href', '/named-routes/') .assert.attributeContains('li:nth-child(2) a', 'href', '/named-routes/foo') .assert.attributeContains('li:nth-child(3) a', 'href', '/named-routes/bar') + .assert.attributeContains('li:nth-child(4) a', 'href', '/named-routes/parent') + .assert.attributeContains('li:nth-child(5) a', 'href', '/named-routes/parent') .assert.containsText('p', 'Current route name: home') .assert.containsText('.view', 'Home') @@ -16,6 +18,13 @@ module.exports = { .assert.containsText('p', 'Current route name: foo') .assert.containsText('.view', 'Foo') + // check parent + .click('li:nth-child(4) a') + .assert.urlEquals('http://localhost:8080/named-routes/parent') + .assert.containsText('p', 'Current route name: parent') + .assert.containsText('.view', 'Parent') + .assert.containsText('.child-view', 'Child') + .click('li:nth-child(3) a') .assert.urlEquals('http://localhost:8080/named-routes/bar') .assert.containsText('p', 'Current route name: bar') @@ -26,6 +35,20 @@ module.exports = { .assert.containsText('p', 'Current route name: home') .assert.containsText('.view', 'Home') + // check child + .click('li:nth-child(5) a') + .assert.urlEquals('http://localhost:8080/named-routes/parent') + .assert.containsText('p', 'Current route name: child') + .assert.containsText('.view', 'Parent') + .assert.containsText('.child-view', 'Child') + + // check parent again but this time the route should not change + .click('li:nth-child(4) a') + .assert.urlEquals('http://localhost:8080/named-routes/parent') + .assert.containsText('p', 'Current route name: child') + .assert.containsText('.view', 'Parent') + .assert.containsText('.child-view', 'Child') + // check initial visit .url('http://localhost:8080/named-routes/foo') .waitForElementVisible('#app', 1000) From 272f703a4e24984a38ae2bd17f42fa87a4d2afcb Mon Sep 17 00:00:00 2001 From: Marc van der Meulen Date: Sat, 13 Aug 2016 03:06:37 +0200 Subject: [PATCH 2/3] Fix code styles --- src/create-matcher.js | 8 ++++---- src/create-route-map.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/create-matcher.js b/src/create-matcher.js index 1725f2c0a..0555ad145 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -30,7 +30,7 @@ export function createMatcher (routes: Array): Matcher { const { name } = location if (name) { - const record = findNamedRecord(nameMap,name) + const record = findNamedRecord(nameMap, name) if (record) { location.path = fillParams(record.path, location.params, `named route "${name}"`) return _createRoute(record, location, redirectedFrom) @@ -205,10 +205,10 @@ function getFullPath ({ path, query = {}, hash = '' }) { return (path || '/') + stringifyQuery(query) + hash } -function findNamedRecord(nameMap: RouteMap, name: string): ?RouteRecord { +function findNamedRecord (nameMap: RouteMap, name: string): ?RouteRecord { const defaultRecord = nameMap['_' + name] - if(defaultRecord) { + if (defaultRecord) { return defaultRecord } return nameMap[name] -} \ No newline at end of file +} diff --git a/src/create-route-map.js b/src/create-route-map.js index fd6883c44..4309633ff 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -61,7 +61,7 @@ function addRouteRecord ( pathMap[record.path] = record if (name) nameMap[name] = record - if (path == '' && parent && parent.name) { + if (path === '' && parent && parent.name) { record.path = normalizePath(record.path) nameMap['_' + parent.name] = record } From 8f8308b2890edf0689777deba302e5f1fe078b01 Mon Sep 17 00:00:00 2001 From: Marc van der Meulen Date: Sat, 13 Aug 2016 03:32:35 +0200 Subject: [PATCH 3/3] fix more styling issues --- src/create-route-map.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index 4309633ff..5aaede3e8 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -62,8 +62,7 @@ function addRouteRecord ( if (name) nameMap[name] = record if (path === '' && parent && parent.name) { - record.path = normalizePath(record.path) - nameMap['_' + parent.name] = record + addDefaultRoute(nameMap, parent.name, record) } } @@ -73,3 +72,9 @@ function normalizePath (path: string, parent?: RouteRecord): string { if (parent == null) return path return cleanPath(`${parent.path}/${path}`) } + +function addDefaultRoute (nameMap: RouteMap, name: string, record: RouteRecord) { + record.path = normalizePath(record.path) + const defaultName = '_' + name + nameMap[defaultName] = record +}