fabien
fabien

Reputation: 2258

Meteor iron-router : cancel a route

Some routes in my application are game and I want to implement a confirmation process when the user is about to leave a runing game.

Is there a way to "stop" the router ?.

I tried that :

Router.configure({

    onBeforeRun: function() {
        Router.stop();
    }
});

Bu it destroys evrything.

Any idea ?

Upvotes: 2

Views: 606

Answers (2)

Manov
Manov

Reputation: 69

This is an 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');
           }
        }
    }
}

Upvotes: 1

SkinnyGeek1010
SkinnyGeek1010

Reputation: 536

Could you use something like this? (I haven't used Iron-Router myself yet)
Basically just catching the links click event, and throwing a confirm dialog. If they click yes it will run the router's go method which triggers the route change, otherwise the click is ignored

edit updates code to use original href value instead

Meteor.templateName.events({
  'click .someLink': function(e) {
    e.preventDefault();

    if (game.isRunning && confirm('are you sure you want to leave?') ) {
      // Router.go('routeName');  // edit, see comment below
      Router.url(e.target.href);
    }     

  }
});

Upvotes: 2

Related Questions