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

Returning false from canDeactivate removes history entry (browser history back) #302

Closed
piwnik opened this issue Mar 8, 2016 · 5 comments
Labels

Comments

@piwnik
Copy link

piwnik commented Mar 8, 2016

When navigating back by browser history and return false from canDeactivate, then history record is removed (replaced).

How to reproduce:

  1. run skeleton-navigation application
  2. Go to "Child Router" page
  3. Go to "Github Users" page
  4. Go to "Welcome" page
  5. Change first name
  6. Press browser back
  7. In promt choose Cancel
  8. Press browser back again
  9. In promt choose Ok

Effect:
"Child Router" page is open instead of "Github Users"

Additional:
If you repeat 6-7 steps few times more, application will close without promt, since application history becomes empty.

Question:
Is any way to do not allow deactivate without loosing navigation history?

It will be logical, for example if I have "users" page, then go to user editing, edit something without saving changes, on browser back I'm asked if I want to leave without saving changes, I choose - no, then save changes, and press history back again. In such case I expect return back to "users" page instead of somewhere else where I was earlier.

@piwnik
Copy link
Author

piwnik commented Mar 8, 2016

I have workaround for this issue, but it seems dirty...

Idea is return redirect to previous page to avoid loosing navigation history.

How to apply:
in welcome.js
import router and redirect

import {Router, Redirect} from 'aurelia-router';

inject router

static inject(){
    return [Router];
}
constructor(router){
    this.router = router;
}

in canDeactivate change

return confirm('Are you sure you want to leave?');

to

 if(!confirm('Are you sure you want to leave?')){
      return new Redirect(this.router.history.previousLocation, {trigger:false, replace: false});
  }

And still, is there some non dirty way to achieve same behavior?

@ggrimbert
Copy link

Hi,

I am experiencing the same problem, thanks for your workaround, it saves my day !

@jwahyoung jwahyoung added the bug label Apr 24, 2016
@classicalConditioning
Copy link

classicalConditioning commented Jun 3, 2016

as another not very clean temp workaround - you can call this.router.history.navigateBack() along with returning false in canDeactivate(). so something like this:

if(!confirm('Are you sure you want to leave?')){ this.router.history.navigateBack(); return false; }

@EisenbergEffect
Copy link
Contributor

The history entry that is being removed should be the one that was cancelled. So, it makes sense for that to be removed, given that that route was never actually allowed to activate or be visited. If you want different behavior, then using a redirect is a perfectly valid solution.

@gama410
Copy link

gama410 commented Jan 5, 2018

I agree with @EisenbergEffect 's comment to some extent. While it seems perfectly valid to me that a cancelled navigation should not have an history entry, removing an entry from the history stack seems illogical. An entry that is in the stack has already been visited, it's not one that is being added. If the entry is removed, the history is left inconsistent (in this case, doing back again would look like jumping 2 steps in the history).

IMO, This is a duplicate of #528 which has been assigned to @davismj

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

No branches or pull requests

6 participants