3

Is there a standard way in jQuery to publish and subscribe to custom class events? There are countless examples of "custom events" which are always related to some HTML element. But what if the javascript class Person() want's to publish and fire an event iAmSick which is listened for by a class HealthProfessional()? I guess it could be done with callbacks but isn't there something like asEvented?

3
  • Well, you could use the window object to fire all events at it, and bind all event handlers to it... The corresponding event object can then contain information about the class that fired the event, etc. Commented Aug 28, 2011 at 17:43
  • Yes you could leverage jQuery that way, but it's not clear to me why that'd be any easier than just writing a simple dispatcher independent of jQuery. I've worked on and with a simple event pub/sub and in JavaScript that's really an extremely simple thing to set up. Good question though :-) Commented Aug 28, 2011 at 17:46
  • Sime and @Pointy, if you could provide examples/links it would help. Commented Aug 30, 2011 at 5:42

2 Answers 2

2

jQuery is a cross-browser JavaScript library that provides abstractions for DOM traversal, event handling, animation, and Ajax interactions for rapid web development.

So the short answer is: no. Thre is no "standard way in jQuery to publish and subscribe to custom class events".

However, if you want to create your own solution, but don't want to start from scratch, I'd highly recommend that you read Darcy Clarke's article:

Library Agnostic Pub/Sub (Publish / Subscribe).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's certainly pointed me to a wealth of information on the topic. I'd always assumed jQuery, like the retired prototype.js, was more than a DOM manipulator but rather a JavaScript library and enhancer.
0

jQuery is a DOM-oriented library, to the extent that the maintainers generally resist attempts to introduce general-purpose programming tools such as the pub/sub system you describe.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.