It’s an easy example as following:
There are many avatars and only one avatar can be selected each time.
I have a model ‘Avatar’, collection ‘Avatars’ and model’s view ‘AvatarView’.
Also there is a view named ‘AvatarAppView’ which is responsible to render the app view.
My implementation is as following:
When one avatar is selected, click event is trigged in the model’s view ‘AvatarView’, then it will be selected but cannot make other models unselected.
Are there any nice solutions? Thank you.
Backbone views have
Backbone.Eventsmixed in so views can produce their own events and other views can listen for those events. So yourAvatarViewcan trigger an event when it is selected:and then the
AvatarAppViewcan listen for those events:Then a simple
selectedevent handler forAvatarAppViewto unselect the otherAvatarViews:Demo: http://jsfiddle.net/ambiguous/thRHK/