Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

microevent.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * MicroEvent - to make any js object an event emitter (server or browser)
  3. *
  4. * - pure javascript - server compatible, browser compatible
  5. * - dont rely on the browser doms
  6. * - super simple - you get it immediately, no mystery, no magic involved
  7. *
  8. * - create a MicroEventDebug with goodies to debug
  9. * - make it safer to use
  10. */
  11. /** NOTE: This library is customized for Onsen UI. */
  12. var MicroEvent = function MicroEvent() {};
  13. MicroEvent.prototype = {
  14. on: function on(event, fct) {
  15. this._events = this._events || {};
  16. this._events[event] = this._events[event] || [];
  17. this._events[event].push(fct);
  18. },
  19. once: function once(event, fct) {
  20. var self = this;
  21. var wrapper = function wrapper() {
  22. self.off(event, wrapper);
  23. return fct.apply(null, arguments);
  24. };
  25. this.on(event, wrapper);
  26. },
  27. off: function off(event, fct) {
  28. this._events = this._events || {};
  29. if (event in this._events === false) {
  30. return;
  31. }
  32. this._events[event] = this._events[event].filter(function (_fct) {
  33. if (fct) {
  34. return fct !== _fct;
  35. } else {
  36. return false;
  37. }
  38. });
  39. },
  40. emit: function emit(event /* , args... */) {
  41. this._events = this._events || {};
  42. if (event in this._events === false) {
  43. return;
  44. }
  45. for (var i = 0; i < this._events[event].length; i++) {
  46. this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1));
  47. }
  48. }
  49. };
  50. /**
  51. * mixin will delegate all MicroEvent.js function in the destination object
  52. *
  53. * - require('MicroEvent').mixin(Foobar) will make Foobar able to use MicroEvent
  54. *
  55. * @param {Object} the object which will support MicroEvent
  56. */
  57. MicroEvent.mixin = function (destObject) {
  58. var props = ['on', 'once', 'off', 'emit'];
  59. for (var i = 0; i < props.length; i++) {
  60. if (typeof destObject === 'function') {
  61. destObject.prototype[props[i]] = MicroEvent.prototype[props[i]];
  62. } else {
  63. destObject[props[i]] = MicroEvent.prototype[props[i]];
  64. }
  65. }
  66. };
  67. window.MicroEvent = MicroEvent;
  68. export default MicroEvent;