fabien fabien - 7 months ago 19
Javascript Question

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 ?

Answer

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);
    }     

  }
});