API Docs for: 3.10.3
Show:

File: test/js/EventTarget.js

  1.  
  2. /**
  3. * Simple custom event implementation.
  4. * @namespace Test
  5. * @module test
  6. * @class EventTarget
  7. * @constructor
  8. */
  9. YUITest.EventTarget = function(){
  10.  
  11. /**
  12. * Event handlers for the various events.
  13. * @type Object
  14. * @private
  15. * @property _handlers
  16. * @static
  17. */
  18. this._handlers = {};
  19.  
  20. };
  21. YUITest.EventTarget.prototype = {
  22.  
  23. //restore prototype
  24. constructor: YUITest.EventTarget,
  25. //-------------------------------------------------------------------------
  26. // Event Handling
  27. //-------------------------------------------------------------------------
  28. /**
  29. * Adds a listener for a given event type.
  30. * @param {String} type The type of event to add a listener for.
  31. * @param {Function} listener The function to call when the event occurs.
  32. * @return {void}
  33. * @method attach
  34. */
  35. attach: function(type, listener){
  36. if (typeof this._handlers[type] == "undefined"){
  37. this._handlers[type] = [];
  38. }
  39.  
  40. this._handlers[type].push(listener);
  41. },
  42. /**
  43. * Adds a listener for a given event type.
  44. * @param {String} type The type of event to add a listener for.
  45. * @param {Function} listener The function to call when the event occurs.
  46. * @return {void}
  47. * @method subscribe
  48. * @deprecated
  49. */
  50. subscribe: function(type, listener){
  51. this.attach.apply(this, arguments);
  52. },
  53. /**
  54. * Fires an event based on the passed-in object.
  55. * @param {Object|String} event An object with at least a 'type' attribute
  56. * or a string indicating the event name.
  57. * @return {void}
  58. * @method fire
  59. */
  60. fire: function(event){
  61. if (typeof event == "string"){
  62. event = { type: event };
  63. }
  64. if (!event.target){
  65. event.target = this;
  66. }
  67. if (!event.type){
  68. throw new Error("Event object missing 'type' property.");
  69. }
  70. if (this._handlers[event.type] instanceof Array){
  71. var handlers = this._handlers[event.type];
  72. for (var i=0, len=handlers.length; i < len; i++){
  73. handlers[i].call(this, event);
  74. }
  75. }
  76. },
  77.  
  78. /**
  79. * Removes a listener for a given event type.
  80. * @param {String} type The type of event to remove a listener from.
  81. * @param {Function} listener The function to remove from the event.
  82. * @return {void}
  83. * @method detach
  84. */
  85. detach: function(type, listener){
  86. if (this._handlers[type] instanceof Array){
  87. var handlers = this._handlers[type];
  88. for (var i=0, len=handlers.length; i < len; i++){
  89. if (handlers[i] === listener){
  90. handlers.splice(i, 1);
  91. break;
  92. }
  93. }
  94. }
  95. },
  96. /**
  97. * Removes a listener for a given event type.
  98. * @param {String} type The type of event to remove a listener from.
  99. * @param {Function} listener The function to remove from the event.
  100. * @return {void}
  101. * @method unsubscribe
  102. * @deprecated
  103. */
  104. unsubscribe: function(type, listener){
  105. this.detach.apply(this, arguments);
  106. }
  107.  
  108. };
  109.