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

  • Home
  • SEARCH
  • 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 8682683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:50:26+00:00 2026-06-12T21:50:26+00:00

This is a simple problem for beginner but so far I haven’t seen a

  • 0

This is a simple problem for beginner but so far I haven’t seen a solution that fits my need. Basically I got a simple menu with ul and li. There are 2 requirements:

Req1: When click on one, the li will get new class .active.

Req2: Menu items are dynamic, meaning I should be able to add or remove any menu item (by using some other button).

There are 2 ways to do this:

Method 1: Tranversing for each MenuView as MenuItem

I have a MenuView with something like this

el:  $('li'),

events: {
  "click" : "highlight"
},

highlight: function(e) {
  thisParent = $(e.target).parent();
  thisParent.siblings('.active').removeClass('active');
  thisParent.addClass('active');
},

Pro: Easy. This is what I have now.

Con: Dependency on the html structure. What if it’s changed to div instead with many layers.

Method 2: One View for the MenuCollection

Create a MenuItemCollection and use MenuView for that collection instead. The el for MenuView will be ul (instead of li). The HTML will look like this with separate id:

<ul>
    <li id="leftmenu-one">one</li>
    <li id="leftmenu-two">two</li>
    <li id="leftmenu-three">three</li>
</ul>

Then when a click event is detected, do 2 things:

2a. Remove all .active class in ul li

2b. Add .active class to the e.target DOM

Pro: Decoupling html design

Con: Little more code.

QUESTION: I think most people would say Method 1 is bad. But is there method 3, 4, 5… that are better? How to handle the adding of new menu item?

  • 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-12T21:50:28+00:00Added an answer on June 12, 2026 at 9:50 pm

    Create a menu item model

        var MenuItem = Backbone.Model.extend({
            title: 'Default Title',
            isSelected: false
        });
    

    and items collection, that would listen to any model selection change event

       var MenuItemCollection = Backbone.Collection.extend({
            model: MenuItem,
    
            initialize: function() {
                this.on('change:isSelected', this.onSelectedChanged, this);
            },
    
            onSelectedChanged: function(model) {
                this.each(function(model) {
                    if (model.get('isSelected') === true && !model.hasChanged('isSelected')) {
                        model.set({isSelected: false});
                    }
                });
            }
        });
    

    After that create a view each for menu item

       var MenuItemView = Backbone.View.extend({
            tagName:  'li',
            events: {
              'click' : 'highlight'
            },
    
            initialize: function() {
                _.bindAll(this);
                this.model.on('change:isSelected', this.onSelectedChanged);
            },
    
            render: function() {
                this.$el.text(this.model.get('title'));
                return this;
            },
    
            onSelectedChanged: function() {
                if (this.model.get('isSelected') === true) {
                    this.$el.addClass('active');
                }
                else {
                    this.$el.removeClass('active');
                }
            },
    
            highlight: function() {
                this.model.set({isSelected: true});
            }
        });
    

    and menu itself like

       var MenuView = Backbone.View.extend({
            tagName:  'ul',
    
            initialize: function() {
                _.bindAll(this);
            },
    
            render: function() {
                this.collection.each(function(model) {
                    var item = new MenuItemView({model: model});
                    this.$el.append(item.render().el);
                }, this);
    
                return this;
            }
        });
    

    Full working js fiddle with comments at http://jsfiddle.net/Kf3SS/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a really simple problem, but I couldn't find a solution anywhere. I'm
hey guys having this really simple problem but cant seem to figure out have
Maybe this is a simple problem, but I am just not seeing it :)
I'm guessing this is a simple problem, but I'm just learning... I have this:
this may seem like a simple problem but I couldn't find it in the
This is such a simple problem, but I can't find an answer anywhere... I
this must be such a simple problem but can someone tell me why this
This question may be simple for experts but for a beginner like me it
This is probably a simple problem to the scala educated mind but I'm still
This seems like a simple problem but nothing nothing is fixing it. I'm trying

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.