To understand how Backbone handles routes lets review some chunk of code from Backbone.History
:
if (this._hasPushState) {
Backbone.$(window).on('popstate', this.checkUrl);
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
Backbone.$(window).on('hashchange', this.checkUrl);
} else if (this._wantsHashChange) {
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
}
If you are using modern browser the 2nd if black will subscribe to hashChange
event and any time you will navigate from link to link it will call checkUrl
which will manage call to appropriate action.
So direct answer to your queastion will be no, you can't.
But you can use a few workarounds depending what you doing in your actions.
1st solution.
Use setTimeout()
or similar _.debounce()
function. Select all links which need to be protected or single treathed and navigate to needed url mnually:
$(document.body).on('click', 'a:not([reference-to-out])', function(e){
e.preventDefault();
if (app.linkTimeoutId) {
clearTimout(app.linkTimeoutId);
}
app.linkTimeoutId = setTimeout(function () {
app.linkTimeoutId = null;
app.router.navigate(e.currentTarget.pathname, {trigger: true});
}, 500)
});
2nd solution
If your heavy tasks are network related (fetching collection, doing another requests to remote), track them keeping in another object and cancel them when Backbone.Router fires 'route' event. This will prevent a lot of needless operations.
3rd solution
I was thinking about $.Deferred
objects, but in this case you should wrap all your controller's actions with it and also keep track of them.