Backbone.JS Router Initialize()

The Backbone.JS Router Initialize() method creates a new constructor. This purpose is served for the router installation.

Syntax:

new Router (options)   

Parameters:
options: This parameter is used to specify the values to be passed to the function.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<title>Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
<script type="text/javascript">
var X = Backbone.Router.extend({
routes: {
'': 'route_1',
'R2': 'route_2'
},
route_1: function(){
document.write("Called Route 1.");
},
route_2: function(){
document.write("Called Route 2.");
}, });
var Y = new X;
Backbone.history.start();
</script>Called Route 1.
<title>Example</title> <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script> <script type="text/javascript"> var X = Backbone.Router.extend({ routes: { '': 'route_1', 'R2': 'route_2' }, route_1: function(){ document.write("Called Route 1."); }, route_2: function(){ document.write("Called Route 2."); }, }); var Y = new X; Backbone.history.start(); </script>Called Route 1.
  

  
Example  
  
  
  
  
  
Called Route 1.  
  


  

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Called Route 1.
Called Route 1.
Called Route 1.

Explanation:
In the above example, the new keyword is the instantiation of the router.