Routes are an easy way to link URLs to execute specific Javascript code. This could be useful, for example, for defining specific tasks for your app:
http://some.site.com/my-sales-order-app/#show-so-details/SO-12345
In the above example, you could have the route show-so-details map to a specific javascript function in your code that would show more details of sales order #SO-12345:
var router = Backbone.Router.extend({
routes: {
"help": "help", // #help
"dosomething/:withparameter": "dosomething", // #dosomething/someparameter
"dosomething/:withparameter(/:withoptionalparameter)": "dosomething" // #dosomething/someparameter/someoptionalparameter
"show-so-details/:sonumber": "handlesodetail", // #show-so-details/SO-12345
},
help: function () {
console.log('help');
},
dosomething: function (withparameter, withoptionalparameter) {
console.log(withparameter, withoptionalparameter);
},
handlesodetail: function (sonumber) {
// Handle SO detail
}
});
var someRouter = new router();
Backbone.history.start();
In the above example, the route will be handled by the handlesodetail function. Now, if there’s a reason you need to “re-trigger” the above URL, you’ll have to use the following:
var resetRouter = function () {
var hash = window.location.hash;
var someRouter = new router();
Backbone.history.stop();
window.location.hash = hash;
Backbone.history.start();
};
The resetRouter function will “reset” your Backbone router and allow you to execute the show-so-details route again. To implement, simply call the resetRouter function at the end of your handler:
handlesodetail: function (sonumber) {
// Handle SO detail code goes here
resetRouter();
}