Over a million developers have joined DZone.

Inheriting Views in Backbone JS

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

BackboneJS provides an awesome technique for structuring our application code. And RequiresJS makes it modular. So we can write separate modules for each HTML block on our page. But sometimes separate views in our application have similar kind of functionalities. Most of the functions in both views are common. So it is not at all recommended to write that common code in both of the Backbone views. It will make application difficult to maintain. To avoid this we can implement a simple OOP technique known as 'Inheritance'. We can inherit one of the view in other view. So the child view will have all the functionalities of the parent view. Additionally we can add our own methods in the child view or we can override the parent methods in child view by creating methods with same name.

Live Demo Download Script 

Here is an simple Backbone view:

define(function (require) {

   return Backbone.View.extend({
       el: $("#parent"),
       initialize: function (opts) {
           console.log("Parent view initialized.");

       events: {
         "click .dad" : "buttonHandler"

       render: function () {
           var html = "<button class='btn dad'>I am Dad</button>";

       buttonHandler: function() {
           $(this.el).append("<div class='message'>I exist in Parent</div>");

This view just adds a button to the page. This button has a handler which adds some HTML to our page. Nothing special happens here. No suppose we have one more view which performs similar kind of functionality. So we will create second view which will inherit the 'viewParent' view class. 

define(function (require) {

   //Include the parent view
   var parentView = require("views/viewParent");

   //Inherit the parent view
   return parentView.extend({
       el: $("#child"),
       initialize: function(opts) {
           //Call the parent's initialize method
           parentView.prototype.initialize.apply(this, [opts]);
           console.log("Child view initialized.");

       events: {
         "click .child" : "buttonHandler"  //This function does not exist in this inherited view

       render: function () {
           var html = "<button class='btn child'>I am Son</button>";


As you can see that we have required the 'viewParent' class and extended that using .extend() method of underscore. Now this child view will have all the features of the parent class. You can see that the click handler function '.buttonHandler' does not exist in child class, still it has been used in it. If the method is not found in the child class, it will be searched up in the parent class thus leading to inheritance.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Arvind Bhardwaj. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}