jQuery events from non-DOM objects (supports chaining)


/ Published in: jQuery
Save to your folder(s)

**Important: this snipplet has moved to Github.**

- [jQuery events from non-DOM objects (supports chaining)](https://gist.github.com/1973168)


Copy this code and paste it in your HTML
  1. /************************
  2.  * Class definition
  3.  ************************/
  4. var MyObjectClass = function(parameters){
  5.  
  6. var $ = jQuery;
  7. var context = this;
  8.  
  9.  
  10. /**
  11. * Detached element used to tap into the regular jQuery events model.
  12. * @private
  13. */
  14. var _events = $(document.createElement('span'));
  15.  
  16. /**
  17. * Attaches a handler to an event for the class instance.
  18. * @param eventType String A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
  19. * @param handler Function A function to execute each time the event is triggered.
  20. * @see http://api.jquery.com/bind/
  21. */
  22. this.bind = function( eventType, handler ){
  23. //_events.bind(eventType, handler); // older jQuery versions
  24. _events.bind(eventType, $.proxy(handler, context)); // jQuery 1.4 and newer
  25. return this;
  26. };
  27.  
  28. /**
  29. * Removes a previously-attached event handler from the class instance.
  30. * @param eventType String A string containing a JavaScript event type, such as click or submit.
  31. * @param handler Function The function that is to be no longer executed.
  32. * @see http://api.jquery.com/unbind/
  33. */
  34. this.unbind = function( eventType, handler ){
  35. //_events.unbind(eventType, handler); // older jQuery versions
  36. _events.unbind(eventType, $.proxy(handler, context)); // jQuery 1.4 and newer
  37. return this;
  38. };
  39.  
  40. /**
  41. * Executes all handlers and behaviors attached to the class instance for the given event type.
  42. * @param eventType String A string containing a JavaScript event type, such as click or submit.
  43. * @param extraParameters String An array of additional parameters to pass along to the event handler.
  44. * @see http://api.jquery.com/trigger/
  45. */
  46. this.trigger = function( eventType, extraParameters ){
  47. _events.trigger(eventType, extraParameters);
  48. return this;
  49. }
  50.  
  51. //
  52. //...
  53. //
  54.  
  55. /**
  56. * Example public method to make the object send an event of type <code>my_event</code>.
  57. */
  58. this.test = function(){
  59. _events.trigger("my_event");
  60. };
  61.  
  62. };
  63.  
  64.  
  65.  
  66. /************************
  67.  * Usage example
  68.  ************************/
  69.  
  70. // Creates an instance of the class
  71. var obj = new MyObjectClass();
  72.  
  73. // Listens to the event the same way you would with DOM elements
  74. obj.bind(
  75. "my_event",
  76. function(e){
  77. console.log("The custom event has been received");
  78. }
  79. );
  80.  
  81. // Makes the object fire an event
  82. obj.test();

URL: http:/www.wildpeaks.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.