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

Before-location-changed event #58

Open
kreinholt opened this issue Oct 6, 2016 · 0 comments
Open

Before-location-changed event #58

kreinholt opened this issue Oct 6, 2016 · 0 comments

Comments

@kreinholt
Copy link

I've been finding it rather difficult to prevent a user from leaving a page if they have unsaved changes since onbeforeunload is never called. I'd like to display a confirmation "Are you sure you want to leave? Unsaved changes will be lost" before letting them leave.

One solution could be to add a before-location-changed event that is cancelable in the _updateUrl and _globalOnClick methods just before a state is pushed/replaced.

if (this.fire("before-location-changed",{},{cancelable:true}).defaultPrevented){
   return
}

This wouldn't work for back/forward browser navigation and you would still have to fire that event yourself if you wanted to imperatively change the url in the format that is suggested in the docs. If you change the path, query, or hash directly, the event would be called in the _updateUrl, we would just also have to reset those variables if defaultPrevented was true.

Open to other solutions, just figure this is a common issue.

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