Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js constructor/initialize Router

Backbone.js constructor/initialize Router

The Backbone.js constructor/initialize Router is used when we create a new router. We can pass routes hash directly as an option. It takes one parameter which is an object which is passed to initialize the function.

Syntax: 

new Router( options )

Parameter: 

  • option: This parameter is an object which is passed to initialize the function. 

Example1: In this example, we will illustrate The Backbone.js constructor/initialize Router. Here we will use the constructor and initialize the Router which will with the new operator. On match of URL, we write some text on output.

HTML




<!DOCTYPE html>
 
<head>
    <title>BackboneJS constructor/initialize Router</title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
 
<body style="text-align:center;" id="body">
    <h1 style="color: green;">
        neveropen
    </h1>
    <h3>BackboneJS constructor/initialize Router</h3>
    <script type="text/javascript">
        var Router = Backbone.Router.extend({
            routes: {
                '': 'link'
            },
            link: function () {
                document.write("Router has been constructed");
            }
        });
        var router = new Router();
        Backbone.history.start();   
    </script>
</body>
 
</html>


Output:

Backbone.js constructor / initialize Router

Example2: In this example, we will construct a new Router and pass arguments to initialize the function.

HTML




<!DOCTYPE html>
 
<head>
    <title>BackboneJS constructor/initialize Router</title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
 
<body style="text-align:center;" id="body">
    <h1 style="color: green;">
        neveropen
    </h1>
    <h3>BackboneJS constructor/initialize Router</h3>
    <script type="text/javascript">
        var Router = Backbone.Router.extend({
            initialize: function (a) {
                document.write(`Hello ${a['name']}
                        your router successfully created`);
            }
        });
        var router = new Router({ name: "Geeks" });
        Backbone.history.start(); 
    </script>
</body>
 
</html>


Output:

Backbone.js constructor/initialize Router

Reference: https://backbonejs.org/#Router-constructor

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments