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

feat(router): add params to navigate() #539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 44 additions & 4 deletions src/router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {RouteRecognizer} from 'aurelia-route-recognizer';
import {Container} from 'aurelia-dependency-injection';
import {History, NavigationOptions} from 'aurelia-history';
import {buildQueryString, parseQueryString} from 'aurelia-path';
import {NavigationInstruction} from './navigation-instruction';
import {NavModel} from './nav-model';
import {RouterConfiguration} from './router-configuration';
Expand Down Expand Up @@ -223,12 +224,18 @@ export class Router {
*
* @param fragment The URL fragment to use as the navigation destination.
* @param options The navigation options.
* @param params The parameters to be used when populating the url pattern.
*/
navigate(fragment: string, options?: NavigationOptions): Promise<PipelineResult | boolean> {
navigate(fragment: string, options?: NavigationOptions, params?: any): Promise<PipelineResult | boolean> {
if (!this.isConfigured && this.parent) {
return this.parent.navigate(fragment, options);
}

if (params) {
let safeParams = Object.assign({}, params);
fragment = this.applyParams(fragment, safeParams);
}

this.isExplicitNavigation = true;
return this.history.navigate(_resolveUrl(fragment, this.baseUrl, this.history._hasPushState), options);
}
Expand Down Expand Up @@ -266,6 +273,39 @@ export class Router {
return childRouter;
}

/**
* Generates a URL fragment with parameters applied to it.
*
* @param fragment The route fragment whose pattern should have parameters applied to it.
* @param params The route params to be used to populate the route pattern.
* @returns {string} A string containing the generated URL fragment.
*/
applyParams(fragment: string, params: any): string {
let fragments = fragment.split('/');
let consumed = {};
for (let i = 0, ilen = fragments.length; i < ilen; ++i) {
let segment = fragments[i];

// Try to parse a parameter :param?
let match = segment.match(/^:([^?]+)(\?)?$/);
if (match) {
let [, name, optional] = match;
fragments[i] = params[name];
consumed[name] = true;
}
}
fragment = fragments.join('/');

for (let key of Object.keys(consumed)) {
delete params[key];
}
let query = buildQueryString(params);
if (query && query.length) {
fragment += (fragment.indexOf('?') < 0 ? '?' : '&') + query;
}
return fragment;
}

/**
* Generates a URL fragment matching the specified route pattern.
*
Expand Down Expand Up @@ -496,7 +536,7 @@ export class Router {
if (results && results.length) {
let first = results[0];
let instruction = new NavigationInstruction(Object.assign({}, instructionInit, {
params: first.params,
params: Object.assign({}, first.params, parseQueryString(queryString)),
queryParams: first.queryParams || results.queryParams,
config: first.config || first.handler
}));
Expand All @@ -510,7 +550,7 @@ export class Router {
}
} else if (this.catchAllHandler) {
let instruction = new NavigationInstruction(Object.assign({}, instructionInit, {
params: { path: fragment },
params: Object.assign({ path: fragment }, parseQueryString(queryString)),
queryParams: results ? results.queryParams : {},
config: null // config will be created by the catchAllHandler
}));
Expand All @@ -523,7 +563,7 @@ export class Router {
let newParentInstruction = this._findParentInstructionFromRouter(router, parentInstruction);

let instruction = new NavigationInstruction(Object.assign({}, instructionInit, {
params: { path: fragment },
params: Object.assign({ path: fragment }, parseQueryString(queryString)),
queryParams: results ? results.queryParams : {},
router: router,
parentInstruction: newParentInstruction,
Expand Down