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

navigateToRoute does not update currentInstruction immediately #658

Open
brandon-marginalunit opened this issue Jun 10, 2020 · 1 comment

Comments

@brandon-marginalunit
Copy link

brandon-marginalunit commented Jun 10, 2020

"aurelia-framework": "1.3.1",
"aurelia-router": "1.7.1",
this.router.navigateToRoute(
    this.router.currentInstruction.config.name, 
    { foo: "bar" },
    { replace: true }
);

console.log(this.router.currentInstruction.queryParams.foo); // undefined

At the time of this console.log(), the actual URL in the browser has been visibly updated. And the parameter does make it into currentInstruction eventually. But for some reason, router.currentInstruction does not get updated synchronously. This is causing havoc in my logic when I want to do non-destructive changes like the following:

/**
 * Apply one or more route params, keeping the current route 
 * and all other param values.
 */
export function assignRouteParams(router: Router, params: {[key: string]: any}) {
    if (router.currentInstruction != null) {
        router.navigateToRoute(
            router.currentInstruction.config.name ?? "unknown", 
            { ...router.currentInstruction.params, ...router.currentInstruction.queryParams, ...params },
            { replace: true }
        );
    }
}

Here, currentInstruction.queryParams cannot be relied upon to include any changes that have just been made to the params via navigateToRoute, which means things get lost.

Is this a bug? And if not, is there a workaround? I tried adding trigger: true to the options, to no effect.

@brandon-marginalunit
Copy link
Author

Workaround for anyone else who's stuck on this bug, pushing the navigation instruction onto the event loop seems to work:

/**
 * Apply one or more route params, keeping the current route 
 * and all other param values.
 */
export function assignRouteParams(router: Router, params: {[key: string]: any}) {
    if (router.currentInstruction != null) {
        setTimeout(() =>
            router.navigateToRoute(
                router.currentInstruction.config.name ?? "unknown", 
                { ...router.currentInstruction.params, ...router.currentInstruction.queryParams, ...params },
                { replace: true }
            ), 0);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant