Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. ;(function(emitter){
  2. "use strict";
  3.  
  4. var bind = function( emitter, event, callback, context, once ) {
  5. var _callback = function() {
  6. if ( once ) {
  7. emitter.off(event, callback);
  8. }
  9.  
  10. /*
  11. * executes the callback in an specific context using parameters sent through emit method
  12. */
  13. return callback.apply(context, arguments);
  14. };
  15. /*
  16. * store the callback like a child of the _callback to facilitate remove an specific callback in an event
  17. */
  18. _callback.callback = callback;
  19.  
  20. return _callback;
  21. };
  22.  
  23. function Emitter() {
  24. this.listeners = [];
  25. }
  26.  
  27. /*
  28. * Store an event to run on future
  29. *
  30. * Usage:
  31. * var emitter = Emitter.create();
  32. *
  33. * emitter.on('click', function(){
  34. * console.log('clicked');
  35. * });
  36. */
  37. Emitter.prototype.on = function( event, callback, context, once ) {
  38. if (!this.listeners[event]) {
  39. this.listeners[event] = [];
  40. }
  41.  
  42. this.listeners[event].push(bind(this, event, callback, context, once));
  43. return this;
  44. };
  45.  
  46. /*
  47. * Store an event to run once on future
  48. *
  49. * Usage:
  50. * var emitter = Emitter.create();
  51. *
  52. * emitter.once('click', function(){
  53. * console.log('clicked');
  54. * });
  55. */
  56. Emitter.prototype.once = function( event, callback, context ) {
  57. return this.on(event, callback, context, true);
  58. }
  59.  
  60. /*
  61. * Remove all callbacks associated with a event or removes an specific callback in event
  62. *
  63. * Usage:
  64. * var emitter = Emitter.create();
  65. *
  66. * emitter.off('click');
  67. * emitter.off('click', function(){});
  68. */
  69. Emitter.prototype.off = function( event, callback ) {
  70. var listeners = this.listeners[event],
  71. count = listeners.length,
  72. i = 0;
  73.  
  74. if (!callback) {
  75. this.listeners = this.listeners.filter(function( evt ) {
  76. return event !== evt && (!callback && callback !== evt.callback);
  77. });
  78. } else {
  79. this.listeners[event] = listeners.filter(function(lst) {
  80. return lst.callback !== callback;
  81. });
  82. }
  83. };
  84.  
  85. /*
  86. * Trigger all callbacks associated with an event
  87. *
  88. * Usage:
  89. * emitter.emit('click');
  90. * emitter.emit('click', argument1, argument2);
  91. */
  92. Emitter.prototype.emit = function() {
  93. var callback,
  94. args = Array.prototype.slice.call(arguments),
  95. event = args.shift(),
  96. listeners = (this.listeners[event] || []).slice(0);
  97.  
  98. while( listeners.length ) {
  99. callback = listeners.shift();
  100. callback(args);
  101. }
  102. };
  103.  
  104. /*
  105. * Creates an emitter instance. How Emitter was created using prototype,
  106. * so all instances will have exactly the same methods
  107. */
  108. emitter.create = function() {
  109. return new Emitter();
  110. };
  111.  
  112. })(this.Emitter = {});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement