Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Add onbeforeunload support #97

Merged
merged 13 commits into from
May 14, 2015

Conversation

gfranko
Copy link

@gfranko gfranko commented May 12, 2015

This handles the situation when a user leaves a particular page and the window.onbeforeunload() method needs to be called.

I had originally created an issue on the fluxible-router repo, but we need this change for both repos.

@yahoocla
Copy link

CLA is valid!

@Vijar Vijar added the ready label May 12, 2015
@jedireza
Copy link
Contributor

Greg pinged me on IM and mentioned that he'll have a similar PR for fluxible-router soon. It may be good to review them in parallel.

@gfranko
Copy link
Author

gfranko commented May 12, 2015

@jedireza Just sent the fluxible-router PR

@jedireza
Copy link
Contributor

cc// @mridgway @lingyan

@mridgway
Copy link
Collaborator

I don't totally understand this use case, but I'm not a fan of handling this in the action. Ideally the components are the only things that deal with window, document or other browser specific code.

@gfranko
Copy link
Author

gfranko commented May 13, 2015

@mridgway This handles the following use case:

  • Warning the user of unsaved changes when he/she hits the back button to navigate to a different route.

Currently, the standard is to set a window.onbeforeunload() method for the pages that need to warn the user before navigating away. This change helps support that.

@gfranko
Copy link
Author

gfranko commented May 14, 2015

Updated the PR to handle cases for both the NavLink and RouterMixin

@mridgway
Copy link
Collaborator

👍 looks good to me. I want @lingyan to take a look before merging.


if (!confirmResult) {
// Pushes the previous history state back on top to set the correct url
self._history.pushState(historyState, pageTitle, (state.route.url || url));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using url as fallback is debatable, since it is going to be the new url after history popstate.

Another tricky thing is about the scroll position, if user set enableScroll to true for RouterMixin, RouterMixin saves the scroll position in history state. In this case of canceling navigate, I wonder if we should add the current scroll position to the historyState object, if enableScroll is true.

@lingyan
Copy link
Contributor

lingyan commented May 14, 2015

@gfranko Thanks for your contribution. Could you add some docs about this?

@gfranko
Copy link
Author

gfranko commented May 14, 2015

@lingyan Yea, I'll add some docs and update the pushState() logic to not be set if there is no state.route.url and the enableScroll logic. Thanks for reviewing!

@gfranko
Copy link
Author

gfranko commented May 14, 2015

@lingyan Updated per your comments. Let me know if there is anything else I should update!

};
var pageTitle = navParams.pageTitle || null;

if (!confirmResult && currentUrl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think currentUrl would be undefined or empty. This && currentUrl adds confusion to the if/else logic, since it does not really make sense to continue with navigation if user has said no (i.e. !confirmResult). I would just remove && currentUrl.

If you are worried about it being falsy, I'd rather check for it inside the if clause and throw an error, since it is not supposed to be that way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it!

@gfranko
Copy link
Author

gfranko commented May 14, 2015

@lingyan

I also sent a PR to fluxible-router with all of these changes.

@lingyan
Copy link
Contributor

lingyan commented May 14, 2015

@gfranko Looks great 👍 I'm merging now. Thanks for your contribution!

lingyan added a commit that referenced this pull request May 14, 2015
@lingyan lingyan merged commit ab9012c into YahooArchive:master May 14, 2015
@lingyan lingyan removed the ready label May 14, 2015
@gfranko
Copy link
Author

gfranko commented May 14, 2015

@lingyan Awesome, thanks!

@lingyan
Copy link
Contributor

lingyan commented May 14, 2015

@gfranko
Copy link
Author

gfranko commented May 14, 2015

👍

lingyan added a commit that referenced this pull request May 14, 2015
Add onbeforeunload support
Conflicts:
	lib/NavLink.js
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants