Best way to make one model 'selected' in a Backbone.js collection?

Viewed 9334

I have a collection of models in my Backbone.js application.

It's a list of items that you can hover over with the mouse or navigate around with the keyboard.

If the mouse is hovering, or if the keyboard navigation has the item selected they will both do the same thing: set that particular item/model to be 'selected'.

So in my model, I have an attribute basically called

selected: false

When it's hovered over, or selected with the keyboard, this will then be

selected: true

But, what is the best way to ensure that when this one model is true, the others are all false?

I'm current doing a basic thing of cycling through each model in the collection and then set the selected model to be true. But I wonder if there is a better, more efficient way of doing this?

5 Answers
Related