Tuesday, March 17, 2009

jQuery event binding blunder

My customer wants a calendar with tricky visual requirements attached, so I thought a good approach would be to:

  • Render a static table with enough room to display any month.
  • Provide all visual data via ajax calls.
  • Create some javascript which decodes the returned json and render the calendar anew.
  • Use jQuery because it’s easy.

Because we need to select a date, and the next or previous months, events are required.

I ended up with the following:

previous.unbind("click").click( function() { current.notifyPrevious();});

The call to unbind() is necessary, I happened to discover. Without it, I bounded an additional, but equivalent event handler for each click.

The next click would trigger all bounded handlers, which would render the calendar and bind new handlers. No wonder my cpu went sky high with that exponential growth.

No comments: