I have a div
generated by a backbone.js view. When the user clicks on this div
, a class active
is added to the div
and the function addToSet
is executed.
Problem: I want another function to be triggered when the View's div
has the class active
. However, my attempt shown below always cause addToSet
function to run when its clicked.
Now, I remove 'click': 'addToSet'
from the events
function, leaving only 'click .active': 'removeFromSet'
. Clicking on the div
does not cause anything to happen! Is this because the event handler cannot select the div
of the view itself, just the elements inside it?
Any idea how I can solve this problem? Thanks!
JS Code
SetView = Backbone.View.extend({
tagName: 'div',
className: 'modal_addit_set',
template: _.template( $('#tpl_modal_addit_set').html() ),
events: {
'click': 'addToSet',
'click .active': 'removeFromSet'
},
initialize: function(opts) {
this.post_id = opts.post_id;
},
render: function() {
$(this.el).html( this.template( this.model.toJSON() ) );
if(this.model.get('already_added'))
$(this.el).addClass('active');
return this;
},
addToSet: function() {
$.post('api/add_to_set', {
post_id: this.post_id,
set_id: this.model.get('id'),
user_id: $('#user_id').val()
});
},
removeFromSet: function() {
$.post('api/remove_from_set', {
post_id: this.post_id,
set_id: this.model.get('id')
});
}
});