DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
  • Front-End: Cache Strategies You Should Know
  • What Is Envoy Proxy?

Trending

  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
  • Front-End: Cache Strategies You Should Know
  • What Is Envoy Proxy?
  1. DZone
  2. Coding
  3. Frameworks
  4. Inheriting Views in Backbone JS

Inheriting Views in Backbone JS

Arvind Bhardwaj user avatar by
Arvind Bhardwaj
·
Dec. 15, 14 · Interview
Like (0)
Save
Tweet
Share
7.33K Views

Join the DZone community and get the full member experience.

Join For Free
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:

//viewParent.js
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>";
           $(this.el).html(html);
       },

       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. 

//viewChild.js
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>";
           $(this.el).html(html);
       },

   });
});

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.

application HTML Awesome (window manager) Blocks Inheritance (object-oriented programming) Object-oriented programming Download

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

Opinions expressed by DZone contributors are their own.

Trending

  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
  • Front-End: Cache Strategies You Should Know
  • What Is Envoy Proxy?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: