Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Created October 28, 2011 06:49
Show Gist options
  • Select an option

  • Save addyosmani/1321768 to your computer and use it in GitHub Desktop.

Select an option

Save addyosmani/1321768 to your computer and use it in GitHub Desktop.
Four ways to do Pub/Sub with jQuery 1.7 and jQuery UI (in the future)

Ben Alman's really tiny pub/sub with my 1.7 updates from https://gist.github.com/1319216. The link to his gist with lots of useful comments is here: https://gist.github.com/661855

/* 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));
@connor
Copy link
Copy Markdown

connor commented Oct 28, 2011

this is awesome.

@gnarf
Copy link
Copy Markdown

gnarf commented Oct 28, 2011

It is pretty awesome, just want to put out a healthy reminder that $.observable is still evolving - $.observer is still a very new child, glad to see a demo of using it for something!

@ajpiano
Copy link
Copy Markdown

ajpiano commented Oct 28, 2011

Yeah, I am pretty sure that the observable stuff won't be available as part of jQuery UI 1.9

@addyosmani
Copy link
Copy Markdown
Author

Updating the gist to reflect correct version information on $.observable and $.observer being available.

@gnarf
Copy link
Copy Markdown

gnarf commented Oct 28, 2011

@addyosamani - $.observer was written very recently by @brado23 - I am pretty sure that it is in flux (but a useful helper for observables, right! :) )

I don't think of $.observer as a way to do pub/sub, that whole concept seems very rooted in a very simple string named "topic" structure. $.observer might help you with a keeping track of data in your storage arrays though, and give ways for other components to listen for change events on specific items or arrays.

@addyosmani
Copy link
Copy Markdown
Author

@gnarf37 that makes sense. Whilst pub/sub is slightly more event driven and $.observer is more data-oriented I consider them both as a part of the same family of observable patterns of development. I should probably come up with a better title for this gist :) Thanks for sharing the extra information about $.observer!. I'm finding out more and more about it as I read and play around with the code.

@pomeh
Copy link
Copy Markdown

pomeh commented Nov 6, 2011

this is nice :)

I would like to know what do you think about this problem. I've write a simple test case on jsFiddle if you want to see it in action and play with it.

Cheers

@furf
Copy link
Copy Markdown

furf commented Jan 11, 2012

Here's another variation where jQuery's event methods are "mixed in" to the supplied object or supplied function's prototype. Also includes a wildcard event for listening to all events. https://github.com/furf/jquery-enable/blob/master/src/bindable.js

@addyosmani
Copy link
Copy Markdown
Author

Niiiice @furf!

@eliperelman
Copy link
Copy Markdown

Hey @addyosmani, your first code snippet has an unused variable method. Just wanted to pass that along.

@addyosmani
Copy link
Copy Markdown
Author

Thanks for pointing that out @eliperelman! :) Fixed.

@eliperelman
Copy link
Copy Markdown

No problem, bud!

@mithun-daa
Copy link
Copy Markdown

Sweet Gist. Noob question. Why does Peter Higgin's pluggin start with a ; ??

@furf
Copy link
Copy Markdown

furf commented Feb 24, 2012

to ensure that in the event of js file concatenation, the javascript will not break if the previous file is missing a trailing semi-colon.

@mithun-daa
Copy link
Copy Markdown

@furf thank you sir. that makes total sense.

@crazy4groovy
Copy link
Copy Markdown

What about $.Deffered()? Can't those be used in some cases? https://tutsplus.com/lesson/deferreds/

@chaslewis
Copy link
Copy Markdown

Thanks! This is a really clear and practical overview of an important topic.
I'd make a slight modification to the first implementation to take topics out of the global space using the module pattern:

$.Topic = (function() {
    var topics = {};
    return function( id ) {
        // ... as above
        return topic;
    };
})();

@Salvodif
Copy link
Copy Markdown

Salvodif commented Feb 7, 2014

nice post

@prologic
Copy link
Copy Markdown

+1 gives me some ideas :)

@Janking
Copy link
Copy Markdown

Janking commented Jun 1, 2015

nice!

@murliatdure
Copy link
Copy Markdown

simple and effective

@gandhirahul
Copy link
Copy Markdown

Simply Amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment