A simple yet under-utilised technique in javascript. A lot of people don't realise that it's not only the browser (click, load, scroll, etc.) that can trigger events.
If you're using this technique a lot then you should probably consider a Pub/Sub library though. Pub/Sub will essentially give you the same functionality (and a similar API) as custom events but the performance of most Pub/Sub libraries is an order of magnitude (or greater) faster than jQuery custom events.
Two thoughts that come to mind when thinking about Pub/Sub libs:
a) jQuery has custom events out-of-the-box and code that uses them looks pretty idiomatic (as far as jQuery goes anyways)
b) I find that when it comes to fancy complex UIs, the events aren't usually the bottleneck, so I'm not sure why performance optimization for them should be a primary design concern (i.e. to the point of considering including/maintaining one more library).
2 comments
[ 2.8 ms ] story [ 18.2 ms ] threadIf you're using this technique a lot then you should probably consider a Pub/Sub library though. Pub/Sub will essentially give you the same functionality (and a similar API) as custom events but the performance of most Pub/Sub libraries is an order of magnitude (or greater) faster than jQuery custom events.
a) jQuery has custom events out-of-the-box and code that uses them looks pretty idiomatic (as far as jQuery goes anyways)
b) I find that when it comes to fancy complex UIs, the events aren't usually the bottleneck, so I'm not sure why performance optimization for them should be a primary design concern (i.e. to the point of considering including/maintaining one more library).