Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery custom events naming conventions

So, what are they?

I mean, I know there is namespacing - but if My event name is too obvious, let say submit, then just adding a namespace submit.myns doesn't prevents triggering it when standard submit is triggered.

Ok, I know, submit is quite bad example, but if I want to use same name as some plugin I'm using? When I have big site, with lots of custom events and lots of plugins, this might be a problem, especially if I want to keep event names simple and meaningful.

So do you use some kind of conventions, like prefixes/postfixes (like myns:event/myns_event)? Or maybe I'm too concerned and my worries are unfounded?

EDIT :

Namespaces are great, if you have to group bounded events for easy unbinding. But the way its working, is no way to be sure that handler click.my is triggered only by click.my. For this, we have to name it different, like my_click - then we keep meaningfull name, we have info that it is our event (and it is less probably that someone uses it), and we still can benefit from standard events namespacing.

Some code to see the differences: http://jsfiddle.net/h2kuN/1/

like image 994
Adam Jurczyk Avatar asked Sep 09 '11 18:09

Adam Jurczyk


1 Answers

Namespacing solves this problem — but only if everybody uses it.

You can bind to and trigger christmas.adam for your seasonal plugin, and I can bind and trigger christmas.bennett and we won't interfere. But if somebody comes along and triggers christmas then both our plugins will be triggered.

This happens because if you bind a handler to event.namespace, then triggering event will trigger your handler. This isn't clear in the jQuery API documentation, but it is true as of jQuery 1.7.1.

So (apart from making it easy to unbind many handlers at once), namespacing serves to protect other code, not yours.

like image 168
Bennett McElwee Avatar answered Oct 23 '22 22:10

Bennett McElwee