Backbone.js extend view is a method under the view class that is used to extend the backbone.js view class in order to create a custom view class. For implementation, first of all, we need to create a custom view class and override the render function if required, also we can specify our own declarative events.
Syntax:
Backbone.view.extend( properties , [classProperties] )
Parameter values:
- properties: It defines the instance properties for the view class.
- classProperties: It defines the class properties of the constructor function attached to it.
Example 1: Basic example for implementing backbone.js extend the view.
HTML
<!DOCTYPE html><html>Â
<head>Â Â Â Â <title>Backbone.js view extend</title>Â Â Â Â Â Â Â Â type="text/javascript"></script>Â Â Â Â <script src=Â Â Â Â Â Â Â Â type="text/javascript"></script>Â Â Â Â <script src=Â Â Â Â Â Â Â Â type="text/javascript"></script></head>Â
<body>    <h1 style="color:green;">        neveropen    </h1>Â
    <button onclick="invoke()">Click me</button>Â
    <script>        var example = Backbone.View.extend({            tagName: "test",            className: "example_display",            events: {            },Â
            initialize: function () {Â
                // Put pre-render processing code here                this.render();            },Â
            render: function () {Â
                // Put html render content here                document.write(                    "<h2>This output is from render function</h2>");            }        });Â
        function invoke() {            var obj = new example();        }    </script></body>Â
</html> |
Output:
Â
Example 2: In this example, we are using a simple boilerplate for the reference implementation.
HTML
<!DOCTYPE html><html>Â
<head>Â Â Â Â <title>Backbone.js view extend</title>Â Â Â Â <script src=Â Â Â Â </script>Â Â Â Â <script src=Â Â Â Â Â Â Â Â Â Â Â Â type="text/javascript">Â Â Â Â </script>Â Â Â Â <script src=Â Â Â Â Â Â Â Â Â Â Â Â type="text/javascript">Â Â Â Â </script></head>Â
<body>    <h1 style="color:green;">neveropen</h1>    <script>        var example = Backbone.View.extend({            tagName: "test",            className: "example_display",Â
            events: {Â
            },Â
            initialize: function () {                //put pre-render processing code here                window.alert('Initialization function invoked');                this.render();            },Â
            render: function () {                //put html render content here                window.alert('render function invoked');            }Â
        });Â
        var obj = new example();    </script></body>Â
</html> |
Output:
Â
Reference: https://backbonejs.org/#View-extend
