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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:58:07+00:00 2026-05-31T09:58:07+00:00

I am stuck on the following issue: I have a model with a property

  • 0

I am stuck on the following issue:

I have a model with a property that defines if it is visibly selected or not, which I will call SelectModel for the purpose of this question.

SelectModel = Backbone.Model.extend({
defaults:{
 isSelected: false
}
})

Now the first part that I do not really get is how I should handle the selection in general.
If I want to use the observer pattern, my View should listen to the change of the isSelected property. But my view also triggers this in the first place, so I would have.

SelectView = Backbone.View.extend({
initialize: function(){
this.model.bind("change:isSelected", this.toggleSelectionVisually)
},

events: {
"click" : toggleSelection
},

toggleSelection: function(){
this.model.set({"isSelected": !this.model.get("isSelected");
},

toggleSelectionVisually:(){
//some code that shows that the view is now selected
},
})

So this in itself already feels a bit absurd but I guess I just understand something wrong.

But the part which I really fail to implement without making my code horrible is handling the selection for multiple models that only one model is selected at a time.

SelectListView = Backbone.View.extend({
initialize: function(){
this.collection = new SelectList();
},

toggleSelection: function(){
????
}   
})

So who should notify whom of the selection change? Which part should trigger it and which part should listen? I am really stuck on this one. For a single View it is doable, for a collection I am sadly lost.

  • 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-31T09:58:09+00:00Added an answer on May 31, 2026 at 9:58 am

    I would have suggested the following simplification for your SelectView until I saw the second part of your question:

    SelectView = Backbone.View.extend({
      events: {
        "click" : toggleSelection
      },
    
      toggleSelection: function(){
        this.model.set({"isSelected": !this.model.get("isSelected");
        //some code that shows whether the view is selected or not
      }
    });
    

    However, since the isSelected attribute is apparently mutually exclusive, can be toggled off implicitly when another one is toggled on, I think the way you have it is best for your case.

    So, using your existing SelectView and, you could have a SelectListView as follows. WARNING: it iterates over your entire collection of models each time one is selected. If you will have a large number of models this will not scale well, and you’ll want to cache the previously-selected model rather than iterating over the entire collection.

    SelectListView = Backbone.View.extend({
      initialize: function(){
        this.collection = new SelectList();
        this.collection.bind('change:isSelected', this.toggleSelection, this);
      },
    
      toggleSelection: function(toggledModel){
        //A model was toggled (on or off)
        if(toggledModel.get('isSelected') {
          //A model was toggled ON, so check if a different model is already selected
          var otherSelectedModel = this.collection.find(function(model) {
            return toggledModel !== model && model.get('isSelected');
          });
    
          if(otherSelectedModel != null) {
            //Another model was selected, so toggle it to off
            otherSelectedModel.set({'isSelected': false});
          }
        }
      }   
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m new to sql and have been stuck on the following issue for almost
I'm stuck with a little issue here, say you have the following code: int
I am trying to learn Backbone.js and have stuck at an issue. model.save is
I'm stuck on following matter: I have a datagrid with 10 items... I also
I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number
I'm new to wicket and stuck with the following problem: I have a table
I'm a beginner programmer and I'm stuck with the following problem: If I have
I'm a little stuck here with a conceptual issue. Assume following [abstracted] setup for
I have a typical problem which is like the following: 1) I was working
I have a model which throws a XMLDocument_Exception. In my controller action I want

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.