Saturday, January 18, 2014

Observer Design Pattern in Jquery


We can use observer design pattern (publisher - subscriber model) in jquery by triggering and attaching events.
Attach a trigger event for publisher
$('div').on('click', function(e) {
    $(this).trigger('MyEvent',e,parameterValue);
});


Attach subscriber methods
$(document).on('MyEvent', function(e,parameterValue) {
    //Do your stuffs with parameterValues..
});

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates