jQuery Fling

Fling is a simple but powerful jQuery plugin for dealing with multiple events using a publisher/subscriber model. jQuery handles the browser-native events, Fling manages them, and you write the code that’s left.

Why Fling?
To understand the purpose of Fling, imagine a standard three-pane email client (to borrow an example from Seth Dillingham, whose work inspired Fling): When you click on a message you just received, several things happen:

  • The mailbox unread count gets decremented
  • The message’s unread indicator goes away
  • The message row text goes from bold to plain
  • The message content appears in the viewer pane

These actions happen with little, if any interaction between each other. All each needs to know is that you clicked on a given unread message. Using jQuery, it’s quite easy to put all four actions inside the same click handler function and be done in minutes. However, when your code base starts to grow, having all that code inside one event handler can make multiple triggers and cascading events cumbersome. There’s a better way to make things happen — Fling your events around.

A Basic Example
Start by including Fling on your page, along with jQuery:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.fling.js"></script>

Then, use jQuery’s handy document ready to set up new Fling subscribers, and finally, publish the event:

// jQuery Document Ready
$(function() {
	// Subscribe to some_event_name
	$.fling('subscribe', 'some_event_name', function() {
		alert('Hello from Fling 1');
	});

	// Subscribe to some_event_name
	$.fling('subscribe', 'some_event_name', function() {
		alert('Hello from Fling 2');
	});

	// Publish some_event_name
	$.fling('publish', 'some_event_name');
});

When run, you should see two alerts pop up in sequence. The only relationship the subscribers have is that they both care about “some_event_name” happening. When publish was called, both did their jobs.

In Depth
$.fling() has four main functions:

  • Create sets up an event without publishing it or subscribing any events. This isn’t used too often, as Create happens automatically when Publish or Subscribe is called.
    $.fling('create', 'myEvent');
  • Destroy frees the memory occupied by the event and it’s subscribers when you’re done with an event for good.
    $.fling('destroy', 'myEvent');
  • Publish notifies all subscribers that a given event happened. It can also pass data to subscribers through a second parameter. Publish can be called at any time, with zero or more subscribers, meaning your event can happen even if no subscribers care about that event.
    $.fling('publish', 'myEvent');
    $.fling('publish', 'myEvent', {my: "data"});
  • Subscribe gets called when an event is Published. You can Subscribe to events that have not been Created or Published yet, as the publisher can arrive later via another script, if needed.
    $.fling('subscribe', 'myEvent', function() {
    	/* ...your code... */
    });
    $.fling('subscribe', 'myEvent', someNamedFunction);
    $.fling('subscribe', 'myEvent', function(event, passedData) {
    	alert(passedData);
    });

Give Fling a Try!
So, that’s it to Fling. There’s not a whole lot to it — you might even be surprised to see just how short the code is. Give it a shot, and see how your jQuery code can be better organized, called from multiple places, and even cascade on down the line.

Download Fling Now

Release History

Released under the MIT License. Fling is short, sweet, and yours to hack. Improvements are welcomed!

15 Responses to “jQuery Fling”

  1. This plugin is so lightweight and awesome. I hope that others can truly see the use of it. Also the way in which it is created is very clever.

  2. Excellent idea! I was JUST writing an application where the requirements continued to change (imagine that) so my event handlers continued to get more verbose so something like this will work out wonderfully. Well done.

  3. w00t! This is just like the GObject signals that I have come to know and love. Thanks!

  4. Looks great – but what are the main advantages over jQuery’s native bind() and trigger() functions? They seem to do something very similar.

  5. @Yoz: jQuery’s bind() and trigger() methods are perfect for single cause-and-effect actions, and I highly recommend them for most cases. Fling is specially suited for situations where one cause can have many effects. Said effects can also beget more “causes”, making events cascade as far as needed.

  6. It’s not quite well-known but you can bind events to plain objects as well:
    var obj={};
    $(obj).bind(‘myevent’,function(){console.log(0)});
    $(obj).triggerHandler(‘myevent’);

  7. Why not call sAction directly like $.fling.create(‘myEvent’); ?
    Really like it btw :)

  8. A simpler method is to use jQuery to bind() custom events on your various elements, then trigger() those events on the document object. Bubbling will take care of the rest.

    Something like:

    $(‘div.inboxCount’).bind(‘inboxEvent’, function(){ do something });

    $(‘a.deleteMessage’).click(function(){ $(document).trigger(‘inboxEvent’) });

  9. I was about to use Tibco’s Pagebus before i saw this. The one thing PB has that’s missing here is to associate data with a subscription which gets passed to the callback.

  10. [...] jQuery Fling | Command-Tab (tags: fling event js jquery) [...]

  11. [...] jQuery Fling Subscribe/Publish event model [...]

  12. [...] jQuery Fling | Command-Tab [...]

  13. [...] 23rd, 2009 in Links jQuery Fling is a simple pub/sub library for web applications. It’s a clean tool for applications with a [...]

  14. [...] on the Dojo publish/subscribe API.” His plugin joins other publish/subscribe plugins such as Fling and jQuery [...]

  15. [...] jQuery Fling | Command-Tab interesting jquery plugin (tags: web development javascript programming webdev code jquery) [...]

Leave a Reply