jQuery event namespacing.
- August 29th, 2011
- Posted in articles
- By Ionut
- Write comment
One day I had a very interesting problem. I created a plugin with a scroll event that I had to add/remove, but I realized I can add the event but then how do I remove just my event? .. a .unbind(‘scroll’) call would remove all scroll events, not only the event added by me.
So after a little digging I found “Namespaced events” support in jQuery. What does this mean?…Well one can add a event within a specific namespace and then remove that events from that namespace.
Here is a example, in which I will add a scoll event then remove my scoll event:
-
//add event
-
$(window).bind('scroll.customNamespace',function() {});
-
-
//remove event
-
$(window).unbind('scroll.customNamespace');
Then event namespacing is very usefull in any plugin development and is a good to know functionality for day to day development.
No comments yet.