I'm new to working with Backbone JS and I'm creating a Backbone View. I made this View so when you click the template it calls function highlight to add the highlight class to my element:
var PlayerView = Backbone.View.extend({
// ...
events: {
"click .player": "highlight"
},
// ...
highlight: function () {
this.$el.find('.player').addClass('highlight');
}
});
I want to make it so when I click anywhere else in the application, I remove the highlight class from this element.
I could declare a click handler on the document and remove the highlight class from there:
$(document).click(function () {
$('.player.highlight').removeClass('highlight');
});
And then use event.stopPropagation() in the highlight function to prevent bubbling:
var PlayerView = Backbone.View.extend({
// ...
highlight: function (evt) {
evt.stopPropagation();
this.$el.find('.player').addClass('highlight');
}
});
This works and exhibits the functionality I'm looking for. However this doesn't exactly leverage the backbone framework. Is there a proper way of doing this in Backbone JS?