Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6235761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:43:35+00:00 2026-05-24T10:43:35+00:00

I have a generic subclass of Backbone.View which has a close event listener. var

  • 0

I have a generic subclass of Backbone.View which has a close event listener.

var GenericView = Backbone.View.extend({

    events : {
        "click .close" : "close"
    },

    close : function () {
        console.log("closing view");
    }

});

I want to subclass this generic class and add some new events. However the below will overwrite the super classes (above) event object. E.g.

var ImplementedView = new GenericView({

    // setting this will stop 'close' in Super Class from triggering
    events : {
        "click .submit" : "submit"
    }

}); 

How should I create a sub class, in this case ImplementedView and retain the events?

I have found one way to achieve this, by extending the event object when the child class is constructed. However I need to re-trigger this.delegateEvents(), which I am guessing is not good. Can any one comment on this?

var ImplementedView = new GenericView({

    initialize : function (options) {

        _.extend(this.events, {
            "click .submit" : "submit"
        });

        // re-attach events
        this.delegateEvents();
    }

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:43:36+00:00Added an answer on May 24, 2026 at 10:43 am

    @Nupul is exactly right: you’re not subclassing your GenericView.

    In fact, subclassing isn’t really the right word here, since JavaScript doesn’t do classical inheritance.

    So let’s first try and understand what’s happening here:

    var GenericView = Backbone.View.extend( propertiesObj, classPropertiesObj )
    

    Backbone.View is a constructor function, that when called with the new keyword, creates a new object for you.

    Since this is JS, all function are really function objects, so Backbone.View.extend is just a function hanging off Backbone.View that does a few things:

    1. It sets up the prototype chain, so you can access properties and call functions defined in the ‘base’ class
    2. It creates and returns a new constructor function you can call (which here would be GenericView) to instantiate objects of your inheriting class
    3. It copies itself to be a function hanging off the constructor function it returns so that you can further inherit.

    So the correct way to set up the protoype chain you want is:

    var ImplementedView = GenericView.extend({
      // implementation goes here
    });
    

    and NOT:

    var ImplementedView = new GenericView({//stuff});
    

    because this just creates a new instance of a GenericView.

    Now, you still have a problem, because when you do something like:

    var impl_view = new ImplementedView;
    
    impl_view.events; // This isn't merged with the events you created
                      // for the GenericView
    

    At this point there are different ways to get the result you want, here’s one that uses delegateEvents kind of like how you did. Using it isn’t bad, incidentally.

    var GenericView = Backbone.View.extend({
    
      genericEvents: { 'click .close': 'close' },
    
      close: function() { console.log('closing view...'); }
    
    });
    
    var ImplView = GenericView.extend({
    
      events: { 'click .submit': 'submit' },
    
      initialize: function(options) {
        // done like this so that genericEvents don't overwrite any events
        // we've defined here (in case they share the same key)
        this.events = _.extend({}, this.genericEvents, this.events);
        this.delegateEvents()
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: Superclass and derived Subclass:Superclass. I have a generic method: public
I have generic list which must be a preserved order so I can retrieve
I have created a subclass of a generic list so that I could implement
I have some confusion about generic programming in java: If Manager is subclass of
Looking to write generic Audit code on my DbContext subclass. foreach (var entry in
Suppose I have a generic class with a generic parameter T which is a
I have a generic class (A) which is to be subclassed a lot like
I have generic type that looks like: public class GenericClass<T, U> where T :
I want to write in Delphi (2009 - so I have generic dictionary class)
I am aware of Web Services and WCF but I have generic question with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.