Skip to content

addRoutes: optional parameter overwriteNames #1129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions dist/vue-router.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var encodeReserveRE = /[!'()*]/g;
var encodeReserveReplacer = function (c) { return '%' + c.charCodeAt(0).toString(16); };
var commaRE = /%2C/g;

// fixed encodeURIComponent which is more comformant to RFC3986:
// fixed encodeURIComponent which is more conformant to RFC3986:
// - escapes [!'()*]
// - preserve commas
var encode = function (str) { return encodeURIComponent(str)
Expand Down Expand Up @@ -399,8 +399,8 @@ function guardEvent (e) {
// don't redirect on right click
if (e.button !== undefined && e.button !== 0) { return }
// don't redirect if `target="_blank"`
if (e.target && e.target.getAttribute) {
var target = e.target.getAttribute('target');
if (e.currentTarget && e.currentTarget.getAttribute) {
var target = e.currentTarget.getAttribute('target');
if (/\b_blank\b/i.test(target)) { return }
}
// this may be a Weex event which doesn't have this method
Expand Down Expand Up @@ -470,11 +470,12 @@ function resolvePath (
base,
append
) {
if (relative.charAt(0) === '/') {
var firstChar = relative.charAt(0);
if (firstChar === '/') {
return relative
}

if (relative.charAt(0) === '?' || relative.charAt(0) === '#') {
if (firstChar === '?' || firstChar === '#') {
return base + relative
}

Expand All @@ -491,11 +492,9 @@ function resolvePath (
var segments = relative.replace(/^\//, '').split('/');
for (var i = 0; i < segments.length; i++) {
var segment = segments[i];
if (segment === '.') {
continue
} else if (segment === '..') {
if (segment === '..') {
stack.pop();
} else {
} else if (segment !== '.') {
stack.push(segment);
}
}
Expand Down Expand Up @@ -540,13 +539,14 @@ function cleanPath (path) {
function createRouteMap (
routes,
oldPathMap,
oldNameMap
oldNameMap,
overwriteNames
) {
var pathMap = oldPathMap || Object.create(null);
var nameMap = oldNameMap || Object.create(null);

routes.forEach(function (route) {
addRouteRecord(pathMap, nameMap, route);
addRouteRecord(pathMap, nameMap, route, undefined, undefined, overwriteNames);
});

return {
Expand All @@ -560,7 +560,8 @@ function addRouteRecord (
nameMap,
route,
parent,
matchAs
matchAs,
overwriteNames
) {
var path = route.path;
var name = route.name;
Expand Down Expand Up @@ -610,7 +611,7 @@ function addRouteRecord (
var childMatchAs = matchAs
? cleanPath((matchAs + "/" + (child.path)))
: undefined;
addRouteRecord(pathMap, nameMap, child, record, childMatchAs);
addRouteRecord(pathMap, nameMap, child, record, childMatchAs, overwriteNames);
});
}

Expand All @@ -621,14 +622,14 @@ function addRouteRecord (
path: alias,
children: route.children
};
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path);
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path, overwriteNames);
});
} else {
var aliasRoute = {
path: route.alias,
children: route.children
};
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path);
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path, overwriteNames);
}
}

Expand All @@ -637,7 +638,7 @@ function addRouteRecord (
}

if (name) {
if (!nameMap[name]) {
if (!nameMap[name] || overwriteNames) {
nameMap[name] = record;
} else if (process.env.NODE_ENV !== 'production' && !matchAs) {
warn(
Expand Down Expand Up @@ -1195,8 +1196,11 @@ function createMatcher (routes) {
var pathMap = ref.pathMap;
var nameMap = ref.nameMap;

function addRoutes (routes) {
createRouteMap(routes, pathMap, nameMap);
function addRoutes (
routes,
overwriteNames
) {
createRouteMap(routes, pathMap, nameMap, overwriteNames);
}

function match (
Expand Down Expand Up @@ -2259,8 +2263,8 @@ VueRouter.prototype.resolve = function resolve (
}
};

VueRouter.prototype.addRoutes = function addRoutes (routes) {
this.matcher.addRoutes(routes);
VueRouter.prototype.addRoutes = function addRoutes (routes, overwriteNames) {
this.matcher.addRoutes(routes, overwriteNames);
if (this.history.current !== START) {
this.history.transitionTo(this.history.getCurrentLocation());
}
Expand Down
44 changes: 24 additions & 20 deletions dist/vue-router.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var encodeReserveRE = /[!'()*]/g;
var encodeReserveReplacer = function (c) { return '%' + c.charCodeAt(0).toString(16); };
var commaRE = /%2C/g;

// fixed encodeURIComponent which is more comformant to RFC3986:
// fixed encodeURIComponent which is more conformant to RFC3986:
// - escapes [!'()*]
// - preserve commas
var encode = function (str) { return encodeURIComponent(str)
Expand Down Expand Up @@ -397,8 +397,8 @@ function guardEvent (e) {
// don't redirect on right click
if (e.button !== undefined && e.button !== 0) { return }
// don't redirect if `target="_blank"`
if (e.target && e.target.getAttribute) {
var target = e.target.getAttribute('target');
if (e.currentTarget && e.currentTarget.getAttribute) {
var target = e.currentTarget.getAttribute('target');
if (/\b_blank\b/i.test(target)) { return }
}
// this may be a Weex event which doesn't have this method
Expand Down Expand Up @@ -468,11 +468,12 @@ function resolvePath (
base,
append
) {
if (relative.charAt(0) === '/') {
var firstChar = relative.charAt(0);
if (firstChar === '/') {
return relative
}

if (relative.charAt(0) === '?' || relative.charAt(0) === '#') {
if (firstChar === '?' || firstChar === '#') {
return base + relative
}

Expand All @@ -489,11 +490,9 @@ function resolvePath (
var segments = relative.replace(/^\//, '').split('/');
for (var i = 0; i < segments.length; i++) {
var segment = segments[i];
if (segment === '.') {
continue
} else if (segment === '..') {
if (segment === '..') {
stack.pop();
} else {
} else if (segment !== '.') {
stack.push(segment);
}
}
Expand Down Expand Up @@ -538,13 +537,14 @@ function cleanPath (path) {
function createRouteMap (
routes,
oldPathMap,
oldNameMap
oldNameMap,
overwriteNames
) {
var pathMap = oldPathMap || Object.create(null);
var nameMap = oldNameMap || Object.create(null);

routes.forEach(function (route) {
addRouteRecord(pathMap, nameMap, route);
addRouteRecord(pathMap, nameMap, route, undefined, undefined, overwriteNames);
});

return {
Expand All @@ -558,7 +558,8 @@ function addRouteRecord (
nameMap,
route,
parent,
matchAs
matchAs,
overwriteNames
) {
var path = route.path;
var name = route.name;
Expand Down Expand Up @@ -608,7 +609,7 @@ function addRouteRecord (
var childMatchAs = matchAs
? cleanPath((matchAs + "/" + (child.path)))
: undefined;
addRouteRecord(pathMap, nameMap, child, record, childMatchAs);
addRouteRecord(pathMap, nameMap, child, record, childMatchAs, overwriteNames);
});
}

Expand All @@ -619,14 +620,14 @@ function addRouteRecord (
path: alias,
children: route.children
};
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path);
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path, overwriteNames);
});
} else {
var aliasRoute = {
path: route.alias,
children: route.children
};
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path);
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path, overwriteNames);
}
}

Expand All @@ -635,7 +636,7 @@ function addRouteRecord (
}

if (name) {
if (!nameMap[name]) {
if (!nameMap[name] || overwriteNames) {
nameMap[name] = record;
} else if (process.env.NODE_ENV !== 'production' && !matchAs) {
warn(
Expand Down Expand Up @@ -1193,8 +1194,11 @@ function createMatcher (routes) {
var pathMap = ref.pathMap;
var nameMap = ref.nameMap;

function addRoutes (routes) {
createRouteMap(routes, pathMap, nameMap);
function addRoutes (
routes,
overwriteNames
) {
createRouteMap(routes, pathMap, nameMap, overwriteNames);
}

function match (
Expand Down Expand Up @@ -2257,8 +2261,8 @@ VueRouter.prototype.resolve = function resolve (
}
};

VueRouter.prototype.addRoutes = function addRoutes (routes) {
this.matcher.addRoutes(routes);
VueRouter.prototype.addRoutes = function addRoutes (routes, overwriteNames) {
this.matcher.addRoutes(routes, overwriteNames);
if (this.history.current !== START) {
this.history.transitionTo(this.history.getCurrentLocation());
}
Expand Down
Loading