Using jQuery bind and trigger to create a basic publisher subscriber pattern
In the html we have as follows:
<div class="sub sub1"></div>
<div class="sub sub2"></div>
Then the following javascript:
$('.sub1').bind('subscriberNotification', function(e, param1, param2) { alert(param1 + ': ' + param2); });
$('.sub2 ').bind('subscriberNotification', function(e, param1, param2) { alert(param2 + ': ' + param1); });
$('.sub').trigger('subscriberNotification', [ 'Hello', 'World' ]);
$('.sub').trigger('subscriberNotification', [ 'Hello', 'World' ]);
We should now receive one alert that says hellow world from subscriber one and one which says world hello from subscriber 2
No comments:
Post a Comment