be29f2d4-f0d3-4dcc-a388-cb3d3c7002ad.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. cc._RF.push(module, 'be29fLU8NNNzKOIyz08cAKt', 'ViewEvent');
  3. // lightMVC/core/base/ViewEvent.ts
  4. "use strict";
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var ViewEvent = /** @class */ (function () {
  7. function ViewEvent() {
  8. // 初始化事件列表
  9. this._eventList = [];
  10. }
  11. /**
  12. * 注册事件
  13. * @param {string} name 事件名称
  14. * @param {(any)=>void} cb 事件回调
  15. * @param {BaseMediator} target 绑定事件的对象
  16. */
  17. ViewEvent.prototype.on = function (name, cb, target) {
  18. var event = new __ViewEvent__(name, cb, target);
  19. for (var _i = 0, _a = this._eventList; _i < _a.length; _i++) {
  20. var e = _a[_i];
  21. if (e.equals(event)) {
  22. console.log("事件[" + name + "]已存在!");
  23. return;
  24. }
  25. }
  26. this._eventList.push(event);
  27. };
  28. /**
  29. * 派发事件
  30. * @param {string} name 事件名称
  31. * @param {Object} body 事件参数,动态参数列表
  32. */
  33. ViewEvent.prototype.emit = function (name, body) {
  34. for (var _i = 0, _a = this._eventList; _i < _a.length; _i++) {
  35. var e = _a[_i];
  36. if (e.name === name) {
  37. e.cb && e.cb.call(e.target, body);
  38. break;
  39. }
  40. }
  41. };
  42. /**
  43. * 移除指定事件
  44. * @param {string} name 事件名称
  45. * @return {boolean} 是否移除
  46. */
  47. ViewEvent.prototype.remove = function (name) {
  48. // 移除指定事件
  49. for (var i = 0; i < this._eventList.length; i++) {
  50. if (name === this._eventList[i].name) {
  51. this._eventList.splice(i, 1);
  52. return true;
  53. }
  54. }
  55. return false;
  56. };
  57. /**
  58. * 销毁接口
  59. */
  60. ViewEvent.prototype.destroy = function () {
  61. };
  62. return ViewEvent;
  63. }());
  64. exports.default = ViewEvent;
  65. /**
  66. * 事件对象结构,用于内部使用。
  67. * @private
  68. */
  69. var __ViewEvent__ = /** @class */ (function () {
  70. /***
  71. * @param {string} name 事件名称
  72. * @param {(...args)=>void} cb 事件回调
  73. * @param {BaseMediator} target 绑定事件的对象
  74. */
  75. function __ViewEvent__(name, cb, target) {
  76. this.name = name;
  77. this.cb = cb;
  78. this.target = target;
  79. }
  80. /**
  81. * 判断两个对象是否相等
  82. * @param {__ViewEvent__} event 目标事件对象
  83. * @return {boolean} 是否相等
  84. */
  85. __ViewEvent__.prototype.equals = function (event) {
  86. return this.name === event.name;
  87. };
  88. return __ViewEvent__;
  89. }());
  90. cc._RF.pop();