How to prevent meteorite router from switching to another page

I have a form page, and I want to warn the user if he leaves it without saving the form. Usually I can achieve this goal by specifying the window of the .onBeforeUnload function.

window.onBeforeUnload = function(){return 'Please save the form before navigating';} 

But it doesn't seem to work in my project using Meteor and Iron Router. Any suggestion?

+5
source share
2 answers

This may be a hacked version, but it works:

 isUnsaved = -> return unless @ready() unless confirm 'Do you really want to leave the page?' Router.go(@url) Router.onStop isUnsaved, only: [ 'editPost' ] 
+1
source

Old post, but just decided. Here is my solution:

 Router.route('/myPath', { unload: function (e, obj) { if(Session.get("hasChanged")){ if (confirm("Are you sure you want to navigate away ?")) { // Go and do some action }else{ // Redirect to itself : nothing happend this.redirect('/myPath'); } } } } 
+1
source

Source: https://habr.com/ru/post/1210655/


All Articles