Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Event for restoration visits? #230

Closed
philipqnguyen opened this issue Jan 20, 2017 · 4 comments
Closed

Event for restoration visits? #230

philipqnguyen opened this issue Jan 20, 2017 · 4 comments

Comments

@philipqnguyen
Copy link

I have a wysiwyg editor on a web page. If the user forgot to save their changes, and they navigate away, I want to prompt them that they have unsaved changes. If they click on other links, I can intercept it with tubolinks:before-visit. However, I cannot find any events that are fired before a restoration visit (back or forward button on the browser).

Are there solutions for this? If not are there plans to add something like turbolinks:before-restore?

@joeldrapper
Copy link

The before-cache event appears to be fired when pressing the back button, so you could possibly use that to trigger a save.

@panoply
Copy link

panoply commented Feb 22, 2018

use turbolinks:before-render event and querySelect your WYSIWYG using the event.data.newBody

@domchristie
Copy link
Collaborator

domchristie commented Sep 13, 2018

This issue has been duplicated by #264, where there is a bit more discussion on the topic.

Turbolinks restoration visits depend on the popstate event which is not cancellable, so unfortunately I don't think displaying a prompt is possible (see also: #216 (comment)). FWIW I have had some success with a different approach: saving form values automatically in session storage, then if the person revisits the form, restore the values from the store. Hope that helps.

@xhafan
Copy link

xhafan commented Jun 7, 2021

@domchristie Thanks for the tip about saving form values into a session storage. I ended up saving the form values into the local storage so it can survive page unloading, with a custom implementation for record versioning and expiration. If anybody is interested in the implementation, you can grab it here.

Previously I had a solution around page unload and turbolinks:before-visit, and the app showed an ugly mix of default browser and custom dialogs, but it could not handle the back and forward buttons. With the saving of values into the local storage, this "dialogs" solution feels pretty lame.

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

No branches or pull requests

5 participants