ILearnable .Net

May 6, 2009

jQuery fires global methods when dispatching events

Filed under: Uncategorized — andreakn @ 07:20

Just found a bug in our app that had me flabbergasted a bit. Turns out the problem was that we had a global javascript function called showCongratulations(){…} that would display a congratulation message to a user who had just won a prize, and we had a jQuery event called ‘showCongratulations’ (which together with the event ‘hideCongratulations’ would control whether these messages were displayed at all, the user could choose to fit his need). So whenever a user chose that he wanted these messages when he won, he would get one immediately. because jQuery would call our global showCongratulations() method when we triggered the ‘showCongratulations’ event.

I don’t exactly know why jQuery does this, as it has a perfectly capable event pub/sub model, might be thought of as a nifty shortcut to have a function auto-subscribe to the events.

anyway: beware that you don’t name the jQuery events things that correlate to global functions, or if you do so that it is a really hacky way (IMHO) to autosubscribe to the correlating event.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: