Oliver Nassar

Recursive Event Bindings

August 12, 2012

My widening-exploration of jQuery has had me uncover a few really-great nuggets, Twitter Bootstrap the most impressive among the bunch. Something I found so interesting in MooTools was the ability to create a class that extended an Events interface. On the surface it was labeled implementing, but in allowed the extending of multiple classes at once, within having to be explicitly.

But what's bringing about this post is an interest I have in nested events. Well, recursive-event-binding, where each event callback receives a callback function itself. The event listener or bind would act as it generally does with that exception, as well as not receiving an Event object of any kind.

Coupling my interest in being able to bind events to any type of object (although in jQuery you are able to do so, so long as you wrap the object in the jQuery symbol $), I came up with the RecursiveEvents library, based on and extending Dustin Diaz' klass library.

I've got some examples and documentation over on that GitHub page, so here I'll focus more on my intention:

I wanted to be able to attach events to type of object without having to find a way around the limitations of the framework. My adoption of jQuery is a somewhat default situation. Because the community is so strong, I sort of need to, but I the syntax still isn't ideal for the more complex code structures I want to develop.

I'm sure a rebuttal could be that I'm not using it write, but that it doesn't have a simple way to create classes, or that most of it's plugins are written as methods that extend an object rather than require the instantiation of one, makes it tough for me to organize my logic.

But that aside, my usage of the klass library is meant to provide a simple-system for creating classes that allow for the recursive-launching of events, with the ability to stay within the scope of the the object that's making the calls (by passing this as a reference to the object itself), and being able to pass-in data through the launch method, to be received by each bound-function.

Worth mentioning is that what finally brought me to Diaz' klass library was John Resig's Simple javascript inheritance library.

While it does essentially the same thing, I opted for Diaz' as it was most recently updated, and gives me the impression it could be modified or debugged more readily than Resig's.

The RecursiveEvents library works with both of them, however. Only the first variable-definition line in RecursiveEvents.class.js needs to be changed.

From:
var RecursiveEvents = klass({

To:
var RecursiveEvents = Class.extend({