Code Monkey home page Code Monkey logo

jquery-tiny-pubsub's Introduction

jQuery Tiny Pub/Sub

A really, really, REALLY tiny pub/sub implementation for jQuery.

(see the original gist)

Getting Started

Download the production version or the development version.

Example usage:

<script src="jquery.js"></script>
<script src="dist/tiny-pubsub.min.js"></script>
<script>
// Creates a "named" logging function.
function createLogger(name) {
  return function(_, a, b) {
    // Skip the first argument (event object) but log the name and other args.
    console.log(name, a, b);
  };
}

// Subscribe to the "foo" topic (bind to the "foo" event, no namespace).
$.subscribe('foo', createLogger('foo'));
// Subscribe to the "foo.bar" topic (bind to the "foo" event, "bar" namespace).
$.subscribe('foo.bar', createLogger('foo.bar'));
// Subscribe to the "foo.baz" topic (bind to the "foo" event, "baz" namespace).
$.subscribe('foo.baz', createLogger('foo.baz'));

// Publish arbitrary values.
$.publish('foo', [1, 2]);
// logs:
// foo 1 2
// foo.bar 1 2
// foo.baz 1 2

$.publish('foo.bar', [3, 4]);
// logs:
// foo.bar 3 4

$.publish('foo.baz', [5, 6]);
// logs:
// foo.baz 5 6

$.unsubscribe('foo.bar');
$.publish('foo', [7, 8]);
// logs:
// foo 7 8
// foo.baz 7 8
</script>

Documentation

Note: Ignore the first argument passed to your subscribed callbacks (the jQuery event object).

Another Note: Previous versions (v0.4+) were written in an attempt to remove the first argument and create a more future-proof API, but unfortunately this resulted in much less elegant, larger and slower code. The point of this plugin is to be TINY, to be used in situations where only size (not performance or usability) is the primary concern (tweets, code golf, etc).**

I frequently see comments about how jQuery's events system has unnecessary overhead that precludes it from being used as the core of a Pub/Sub implementation. The jQuery events system is tried-and-true, having been architected to be both fast and robust, and the vast majority of users of this plugin should never encounter any kind of performance issues.

Because this plugin's $.subscribe, $.unsubscribe and $.publish methods all use the jQuery .on(), .off() and .trigger() methods internally, those methods' complete signatures are available to you.

You can use namespaces for more control over unsubscribing and publishing.

Just use this handy terminology guide (jQuery events term → Pub/Sub term), and everything should make sense:

  • on → subscribe
  • off → unsubscribe
  • trigger → publish
  • type → topic

In addition, should you need it, these methods are fully compatible with the jQuery.proxy() method, in case you not only want more control over to which context the subscribed callback is bound, but want to be able to very easily unsubscribe via callback reference.

Regarding performance: If at some point, your application starts processing so many messages that performance issues start to develop, you could always write a replacement "jQuery Not-So-Tiny Pub/Sub" plugin with the same API and just drop it in as a replacement to this plugin. But keep in mind that you'll also need to add in the aforementioned features, too.

Release History

2013-01-29 - v0.7.0 - First official release.

jquery-tiny-pubsub's People

Contributors

cowboy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-tiny-pubsub's Issues

jQuery.proxy is compatible? how?

Hi,
thanks a lot for awesome pub-sub implementation!
Not sure if it's bug, or if I'm missing something, so let's call this issue a question first. Readme states:
... In addition, should you need it, these methods are fully compatible with the jQuery.proxy() method, in case you not only want more control over to which context the subscribed callback is bound, but want to be able to very easily unsubscribe via callback reference. ...

However, I don't think it does or don't understand how I could bind custom context to subscribed callback. Because it always falls back to the main o object: o.trigger.apply(o, arguments);

Could you please add example of $.proxy usage into readme in the case it's really possible?

Have a nice day,
Luk

PS: Perhaps you mean it is compatible with jQuery.proxy when supplying the callback: $.subscribe('foo', $.proxy(createLogger, $someContext, 'foo'));?
`

Why the license?

Just stumbled on this and noticed it's almost identical to something I've written before (like, a few characters away). Seems a bit backwards to have a license on something that is both obvious and reproducible within a minute. Plus the license is way longer than the code itself! Let it free, no?

Wrong links in README

The current README.md states the wrong links to the raw production and development files.

Callback without the event object

Hi,

I was looking for a simple Pub/Sub system to use with jQuery/jQuery UI and I found "jquery-tiny-pubsub", which is great. However, I don't really like that the functions are called with an event object as first argument.

In your example, you say : "// Skip the first argument (event object) but log the name and other args.".

My example:

$.publish("msg", 5);

$.widget('myWidget', {
    _create: function () {
        var self = this;            
        $.subscribe("msg", $.proxy(self, "_handleRecieve"));
    },  
    _handleRecieve: function (event, value) {
        this.element.append("<p>" + value + "</p>");
    }
});

I don't want the signature of the "_handleRecieve" method to be "function (event, value) ", but "function (value)", because the "event" it's never used.

Therefore, I rewrote the method "subscribe" as I follows:

  $.subscribe = function() {
    var events, handler, handlerWrapper;

    events = arguments[0];
    handler = arguments[1];
    handlerWrapper = function(event) {
    return handler.apply(this, Array.prototype.slice.call(arguments, 1));
    };

    o.on.call(o, events, handlerWrapper);
  };

I would like to know your oppinion on this implementation. Are there any downsides?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.