Sponsored by Gluten Free Waffles and Sliced Bananas

Friday, March 22, 2013

Event Namespacing with jQuery

2:21 PM Posted by Tyson Nero No comments
It's a standard practice to bind and unbind events using general event types such as "click". You would probably do something like this.

However, there are cases, especially with third party libraries for instance, where event collisions could occur. Let's say you bring in a plugin that does some type of magic on a button using click events. You too want to do your own magic and also bind to the click event. Time passes, and for some reason, the plugin gets destroyed and unbinds the click event. Not only has it cleaned up itself, but your event handlers are gone too!

The solution is event namespacing, which is simply adding class names to your events:

Event namespacing comes in extra handy when writing your own plugins. Remember, there are many other plugins out there, and just like writing C# code, you want to define unique namespaces that will hopefully not collide with other libraries.


Post a Comment