components/RelayParent.js

  1. /**
  2. * This component listens for specified local entity messages and re-broadcasts them on its parent entity.
  3. *
  4. * @memberof platypus.components
  5. * @class RelayParent
  6. * @uses platypus.Component
  7. */
  8. import createComponentClass from '../factory.js';
  9. export default (function () {
  10. var broadcast = function () {
  11. var parent = this.owner.parent;
  12. if (parent) {
  13. parent.trigger.apply(parent, arguments);
  14. }
  15. };
  16. return createComponentClass(/** @lends platypus.components.RelayParent.prototype */{
  17. id: 'RelayParent',
  18. properties: {
  19. /**
  20. * This is an object of key/value pairs. The keys are events this component is listening for locally, and the value is the event to be broadcast on the parent. The value can also be an array of events to be triggered on the parent.
  21. *
  22. * "events": {
  23. * "sleeping": "good-night",
  24. * "awake": ["alarm", "get-up"]
  25. * }
  26. *
  27. * @property events
  28. * @type Object
  29. * @default null
  30. */
  31. events: null
  32. },
  33. initialize: function () {
  34. var event = '',
  35. events = this.events;
  36. // Messages that this component listens for and then broadcasts to parent.
  37. if (events) {
  38. for (event in events) {
  39. if (events.hasOwnProperty(event)) {
  40. this.addEventListener(event, broadcast.bind(this, events[event]));
  41. }
  42. }
  43. }
  44. }
  45. });
  46. }());