Pub Sub Pattern using jQuery .on() and .off()

Sam Deering

A quick look at how to implement pub sub technique using jQuery’s on and off functions.

Related Posts:

/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
 * http://benalman.com/
 * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */

(function($) {

  var o = $({});

  $.subscribe = function() {
    o.on.apply(o, arguments);
  };

  $.unsubscribe = function() {
    o.off.apply(o, arguments);
  };

  $.publish = function() {
    o.trigger.apply(o, arguments);
  };

}(jQuery));

source: https://gist.github.com/661855

How it works and how to use it? Working example: http://jsfiddle.net/cowboy/HvAJf/

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

No Reader comments

Comments on this post are closed.