How do I have a widget listen for clicks on other widgets but not listen to normal clicks on itself. For example:
var $dialogWidget = jQuery('#dialogWidget');
$dialogWidget
.bind('click.btn1.otherWidget',doSomething)
.bind('click.btn2.otherWidget',doSomething2)
.bind('click.btn1.otherWidget2',doSomething3);
The problem with this code is that when I click anywhere on $dialogWidget, all of these events that I have bound will be triggered. What I want is this
var $otherWidget = jQuery('#otherWidget');
var $btn1 = $otherWidget.find('a.btn1');
$btn1
.click(function(){
$dialogWidget.trigger('click.btn1.otherWidget');
});
This should be the only way that ‘click.btn1.otherWidget’ event is fired for $dialogWidget.
One possible thing I could do that would be a hack is anytime I am manually firing a click event is to use ‘clicked’ instead of click, but that feels like a hack. Any ideas?
I think the simplest solution is to rename your events, use a name that’s not taken. In this case, just don’t use
click, do something like this instead:Then when you trigger it:
This was
clickwon’t trigger your namespace custom events that you don’t want fired any other way. To be clear this isn’t a hack, it’s completely normal jQuery behavior, it’s intentionally supported throughout the jQuery event model. Google forjQuery custom events, there are a lot of resources out there around this.