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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:16:15+00:00 2026-06-12T18:16:15+00:00

I have a sidebar view with a list of items like so SideBarView =

  • 0

I have a sidebar view with a list of items like so

SideBarView = Backbone.View.extend(
  events:
    "click item": "dosomething"
  render:
    //Render item list from a collection
)

Now, I am trying to display the sidebar view on two different routes but I ran into a little problem. On route A, I want the dosomething() method to print “A”, and on route B I want the dosomething() method to print “B”. So, I was wondering what is the best approach into this problem? I thought about it and came up with the following 2 approaches but neither seem to be elegant enough.

Approach 1

Make another SideBarView and call it SideBarView2 then change method dosomething to dosomethingForRouteB. So then I can use SideBarView2 for route B. However, this violated the DRY principle; not to mention, lets say I want to change the rendering method for the SideBarView later on then I would have to make changes at two different places. Overall, I think this is a very bad approach

Approach 2

Inside the dosomething() method, I can have the logic statement like so

route = getCurrentRoute()
if(route = "/routeA")
 print A
else
 print B

A little more elegant than approach 1 but having a logical statement inside your view is a bit too hacky.

Anyway, my question is that should I use approach 2 or is there a better approach to this problem?

Thanks

  • 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-06-12T18:16:17+00:00Added an answer on June 12, 2026 at 6:16 pm

    Why don’t you let SideBarView accept a parameter of which thing you want to display.

    SideBarView = Backbone.View.extend({
        print_value: B,
    
        events: {...},
    
        initialize: function(options){
            options = options || {};
            if(options.display === 'A'){
                 this.print_value = "A";
            }
         },
    
         render: function(){ ...}
    }
    

    And then in your router:

     route_method: function(path){
         new SideBarView({display: path});
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a module defined as follows: define( ['backbone', 'View/Sidebar', 'View/ControlBar'], function() { ...
I have a details view with a sidebar and I would like to allow
I have the view: class FoursquareSearch.Views.SearchNew extends Backbone.View tagName: 'sidebar' template: JST[templates/search/new_search] #events: #
I have a sidebar that I want to behave like it's frozen on the
Okay, on one of my sites I have a news sidebar that displays events
The Application Layout I have an application, with a sidebar that holds many items
I have a blog that has a sidebar with a partial view in it
I have a login.phtml view that would like to put in a common path
I have a working coffeescript/backbone idiom that looks like this: SidebarWidgets = ((() ->
I have a blog with Dynamic View as the template (The one with Sidebar/Mosaic

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.