Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js extend Router

Backbone.js extend Router

The Backbone.js extend Router is used to extend the Backbone’s Router class in which we can create our own Model. It also facilitates the instance property that are attached to the constructor function of the Router directly. It provide a router hash that pairs routes to action. 

Syntax: 

Backbone.Router.extend(  properties, classProperties );

Parameters: It accepts two parameters that are described below:

  • Parameters: This parameter provides the instance properties for the specified Router class.
  • classProperties: This class property is attached to the Router’s constructor function.

Example 1: In this example we will illustrate The Backbone.js extend Router. We will create our own Router class with the help of extend method.

HTML




<!DOCTYPE html>
<html>
<head>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
  
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
  
    <h3>BackboneJS Router extend</h3>
  
    <script type="text/javascript">
        var Router = Backbone.Router.extend({
  
            routes: {
                '#rout1': 'role1',
                '#rout2': 'role2',
            },
  
            role1: function () {
                document.write(
                  "Illustrating Router extend!!!"
                );
            },
            role2: function () {
                document.write(
                  "Illustrating Router extend!!!"
                );
            }
        });
  
        var appRouter = new Router();
  
        document.write(JSON.stringify(appRouter));
    </script>
</body>
  
</html>


Output:

Backbone.js extend Router

Example 2: In this example we will add some initial function to our custom Router class which will call whenever new instance of class is called.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
  
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
  
    <h3>BackboneJS Router extend</h3>
  
    <script type="text/javascript">
        var Router = Backbone.Router.extend({
  
            routes: {
                '': 'r1',
            },
  
            r1: function () {
                document.write(
                  "Illustrating Router extend!!!"
                );
            }
        });
  
        var appRouter = new Router();
        Backbone.history.start();
    </script>
</body>
  
</html>


Output:

Backbone.js extend Router

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

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