More information about the Underscore mailing list

[_] jquery - triggering all events bound to an object

juan kennaugh contact at jkennaugh.co.uk
Thu May 9 11:24:44 BST 2013

Hi all

I've just been looking into some jquery plugin patterns and found 
somebody binding custom events to an empty object so thought I would 
look into it.

but I'm not quite sure how it works - can anyone tell me how I could 
trigger all the events bound to the object?...




var $events_object = $({}); // an empty, jquery wrapped object

var functions_object = {
     my_custom_event_1: function () {alert('e1')},
     my_custom_event_2: function () {alert('e2')}
}

$.each(functions_object, function(custom_event, func){
     $events_object.bind(custom_event, func);
});

// triggering events explicitly works
$events_object.trigger('my_custom_event_1'); // alerts: 'e1';
$events_object.trigger('my_custom_event_2'); // alerts: 'e2';

// so, how would I be able to trigger all the events on $events_object 
without calling each one explicitly?





Cheers

Juan