fabien fabien - 1 year ago 69
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 :


onBeforeRun: function() {

Bu it destroys evrything.

Any idea ?

Answer Source

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

  'click .someLink': function(e) {

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