Is there any reason why it would be against best practice to use jQuery's trigger
and on
methods as a cheap and easy pubsub?
The following works:
$(document).on('my:custom:event',function(){
alert('this is an event');
});
And later:
$(document).trigger('my:custom:event');
//=> alerts
Obviously a real jQuery pubsub plugin would be trivial to install - I'm really looking to see:
If your use case is simple enough - go right ahead:
You can use it on arbitrary objects
var obj = {};$(obj).on(...) .
Note that if you're using it on the document, you're effectively creating a global hidden dependency. Also, rolling your own pubsub for the client-side is ~20 LoC so that's another consideration since a jQuery dependency isn't something you'd always want to have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With