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 7488753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:01:32+00:00 2026-05-29T15:01:32+00:00

I have several views that have common code I’d like to abstract into a

  • 0

I have several views that have common code I’d like to abstract into a custom Backbone.View class. Is there any best practices for doing this?

is a good pattern to do something like this? :

 // Base Grid view
 var GridView = Backbone.View.extend({
     initialize : function(){
                 //common view init code .. 

                 //do the plug in overrides
                 if (options.addHandler)
                     this.addHandler = options.addHandler;
                 if (options.events)
                     //?? extend default events or override? 
                     this.events = $.extend(this.events, options.events);
                 }, 
     addHandler : function() { 
                //defaulthandler this code can be overridden
                });
});


// in another object create some views from the GridView base
....
var overrides = { events:"xxx yyy", el: ulElement addHandler: myAddFunction } 
var UserList = GridView.extend(overrides)
var userList = new UserList(users, options);
....

var coursesOverrides : {addHandler:  ...}
var coursesOptions:    {el: courseElement, ...}
var CourseList = GridView.extend(coursesOverrides)
var courseList= new CourseList (courses, coursesOptions)

// along the same lines  maybe there's an abstraction for toolbar views 
var ClassToolbarView = ToolbarBase.extend(toolOverrides)
var classtoolbar = new ClassToolbarView(actions, toolbaropts)

Any pointers to good examples of extending a View for refactoring common view code is appreciated.

  • 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-29T15:01:34+00:00Added an answer on May 29, 2026 at 3:01 pm

    First, I don’t see the options being passed in your initializer(), so that’s a bug.

    Secondly, the .extend() method is inherited:

    var GridView = Backbone.View.extend({ ... })
    var GridViewWithNewFunctionsAndEvents = GridView.extend({ ... })
    

    And you can replace or extend GridView’s functionality, and call new GridViewWithNewFunctionsAndEvents() and get the extra functionality in a new object you need, just like you extend the Backbone stock View class.

    If you need to extend the initializer, you can do this to call the initializer on the superclass:

     var GridViewWithNewFunctionsAndEvents = GridView.extend({
        initializer: function(options) {
            GridView.prototype.initializer.call(this, options);
            /* Your stuff goes here */
        }
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view controller class that has to implement several protocols. Too keep
I have some common javascript functionality that I want to share across several views/pages.
I have several views that submit to the same controller's ActionResult and would like
I have several partial views with Javascript that I am trying to move to
I have several ASP.NET MVC 3 Web applications that have a lot of common
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
I'm developing a new desktop application that will have several views such as a
I have a base class and several concrete classes that derive from it. Lets
I have several views inside one main view. Those views are all behind one
I have several queries that use a WITH clause, or Common Table Expression, 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.