Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent navigating to another page in meteor iron router

I have a form page and I want to alert user if he leaves it without saving the form. Typically I can achieve this purpose by stating a function window.onBeforeUnload.

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

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

like image 452
aladine Avatar asked Nov 01 '22 11:11

aladine


2 Answers

This is maybe a hacky 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'
  ]
like image 124
pmuens Avatar answered Dec 06 '22 11:12

pmuens


Old post, but just solved it. 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');
           }
        }
    }
}
like image 41
Manov Avatar answered Dec 06 '22 12:12

Manov