pubSub.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const PubSub = {
  2. handlers: {}
  3. };
  4. PubSub.off = function (eventType, fn) {
  5. const fns = this.handlers[eventType] || [];
  6. if (fn) {
  7. const index = fns.findIndex(_fn => _fn === fn);
  8. if (index > -1) fns.splice(index, 1);
  9. } else {
  10. delete this.handlers[eventType];
  11. }
  12. return this;
  13. };
  14. PubSub.on = function (eventType, handler) {
  15. if (!(eventType in this.handlers)) {
  16. this.handlers[eventType] = [];
  17. }
  18. this.handlers[eventType].push(handler);
  19. return this;
  20. };
  21. PubSub.emit = function (eventType) {
  22. // eslint-disable-next-line prefer-rest-params
  23. const handlerArgs = Array.prototype.slice.call(arguments, 1);
  24. if (!this.handlers[eventType]) return this;
  25. for (let i = 0; i < this.handlers[eventType].length; i++) {
  26. this.handlers[eventType][i].apply(this, handlerArgs);
  27. }
  28. return this;
  29. };
  30. export default PubSub;
  31. /*
  32. * 编辑游客
  33. * */
  34. export const EDIT_TOURIST_ITEM = 'EDIT_TOURIST_ITEM';
  35. /*
  36. * 编辑游客
  37. * */
  38. export const ADD_TOURIST_ITEM = 'ADD_TOURIST_ITEM';
  39. /*
  40. * 删除游客
  41. * */
  42. export const DELETE_TOURIST_ITEM = 'DELETE_TOURIST_ITEM';
  43. export const CHOOSE_TOURIST = 'CHOOSE_TOURIST';