In the past I have done very simple pub / sub in jQuery by binding on the window.
// subscribe
$( window ).on("someEvent", function() { ... });
// publish
$( window ).trigger("someEvent");
However I recently learned about the new callbacks feature, which seems to be the recommended way of handling pub / sub in jQuery.
What are the advantages of using callback, as opposed to just binding on the window? For a simple system like the one above, is using the Callback feature overkill?
Edit: Here is a bit more info about how I commonly use the above...
This is something that I will sometimes do to allow my jQuery plugins to talk to each other. For example, I have my own draggable and droppable plugins that need to communicate.
When dragging starts, updates and stops, the draggable plugin triggers custom events on the window. The droppable plugin watches these events and reacts accordingly.
// in draggable
onStart: function() {
$( window ).trigger("dragger.start", [data]);
}
// in droppable
$( window ).on("dragger.start", function(event, data) {
...
});