Custom Class Events in jQuery?
Is there a standard way in jQuery to publish and subscribe to custom class events? There ar开发者_如何学Goe 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?
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).
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.
精彩评论