emitter.js 845 B

12345678910111213141516171819202122232425262728293031323334
  1. function broadcast(componentName, eventName, params) {
  2. this.$children.forEach(child => {
  3. var name = child.$options.componentName;
  4. if (name === componentName) {
  5. child.$emit.apply(child, [eventName].concat(params));
  6. } else {
  7. broadcast.apply(child, [componentName, eventName].concat(params));
  8. }
  9. });
  10. }
  11. export default {
  12. methods: {
  13. dispatch(componentName, eventName, params) {
  14. var parent = this.$parent || this.$root;
  15. var name = parent.$options.componentName;
  16. while (parent && (!name || name !== componentName)) {
  17. parent = parent.$parent;
  18. if (parent) {
  19. name = parent.$options.componentName;
  20. }
  21. }
  22. if (parent) {
  23. parent.$emit.apply(parent, [eventName].concat(params));
  24. }
  25. },
  26. broadcast(componentName, eventName, params) {
  27. broadcast.call(this, componentName, eventName, params);
  28. }
  29. }
  30. };