Backbone router has an execute method which is called for every route change, we can return false to prevent the current transition. The code will probably look like below :
With an asynchronous popup (untested code, but should work)
Backbone.Route.extend({ execute: function(callback,args){ if(this.lastRoute === 'room'){ showPopup().done(function(){ callback & callback.apply(this,args); }).fail(function(){ Backbone.history.navigate('room/486',{trigger:false}); }); }else{ callback && callback.apply(this,args); } }, showPopup: function(){ var html = "<<div><p>Do you really want to exit</p><button id='yes'>Yes</button><button id='no'>No</button></div>" var promise = $.Deferred(); $('body').append(html); $(document).on('click','button#yes',function(){ promise.resolve(); }); $(document).on('click','button#no',function(){ promise.reject(); }); return promise; } });
With synchronous confirm popup
Backbone.Route.extend({ execute: function(callback,args){ if(this.lastRoute === 'room'){ var conf = confirm("Do you really want to exit the room ?"); if(!conf){ //Change the route back to room Backbone.history.navigate('room/486',{trigger:false}); return false; } }; callback && callback.apply(this,args); } });
References:
http://backbonejs.org/#Router-execute