Skip to content
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

Option to not use array indices in query string #721

Closed
wants to merge 1 commit 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
13 changes: 9 additions & 4 deletions utils/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,21 @@ var Path = {
* Returns a version of the given path with the parameters in the given
* query merged into the query string.
*/
withQuery: function (path, query) {
withQuery: function (path, query, useIndices) {
var existingQuery = Path.extractQuery(path);
var queryString;

if (existingQuery)
if (existingQuery) {
query = query ? merge(existingQuery, query) : existingQuery;
}

var queryString = query && qs.stringify(query);
if (query) {
queryString = decodeURIComponent(qs.stringify(query, {
indices: useIndices
}));

if (queryString)
return Path.withoutQuery(path) + '?' + queryString;
}

return path;
},
Expand Down
18 changes: 17 additions & 1 deletion utils/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ var DEFAULT_LOCATION = canUseDOM ? HashLocation : '/';
*/
var DEFAULT_SCROLL_BEHAVIOR = canUseDOM ? ImitateBrowserBehavior : null;

/**
* Don't serialize array query params indices between the [] in the query string.
*
* For example, serializing { foo: [ 'a', 'b' ] } will turn into:
*
* // when option is set to false:
* // ?foo[]=a&foo[]=b
*
* // when option is set to true:
* // ?foo[0]=a&foo[1]=b
*/
var DEFAULT_USE_QUERY_INDICES = false;

/**
* The default error handler for new routers.
*/
Expand Down Expand Up @@ -132,6 +145,8 @@ function hasMatch(routes, route, prevParams, nextParams, prevQuery, nextQuery) {
* the DOM is available, "/" otherwise
* - scrollBehavior The scroll behavior to use. Defaults to ImitateBrowserBehavior
* when the DOM is available, null otherwise
* - useQueryIndices Whether array query parameters should be serialized with
* indices in the query string.
* - onError A function that is used to handle errors
* - onAbort A function that is used to handle aborted transitions
*
Expand All @@ -152,6 +167,7 @@ function createRouter(options) {
var components = [];
var location = options.location || DEFAULT_LOCATION;
var scrollBehavior = options.scrollBehavior || DEFAULT_SCROLL_BEHAVIOR;
var useQueryIndices = options.useQueryIndices || DEFAULT_USE_QUERY_INDICES;
var onError = options.onError || defaultErrorHandler;
var onAbort = options.onAbort || defaultAbortHandler;
var state = {};
Expand Down Expand Up @@ -238,7 +254,7 @@ function createRouter(options) {
path = route.path;
}

return Path.withQuery(Path.injectParams(path, params), query);
return Path.withQuery(Path.injectParams(path, params), query, useQueryIndices);
},

/**
Expand Down