Guest User

Untitled

a guest
May 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import Component from '@ember/component';
  2.  
  3. const INVALID_ATTRS = ['tagName'];
  4.  
  5. function parseEvent(attr) {
  6. const match = attr.match(/^on-([\w-]+)/);
  7.  
  8. return match && match[1];
  9. }
  10.  
  11. export default Component.extend({
  12.  
  13. didReceiveAttrs() {
  14. this._super();
  15.  
  16. this._parseAttrs();
  17. },
  18.  
  19. didInsertElement() {
  20. this._super();
  21.  
  22. this._addEventListeners();
  23. },
  24.  
  25. _parseAttrs() {
  26. const bindings = [];
  27. const events = {};
  28.  
  29. Object.entries(this.attrs).forEach(([key, value]) => {
  30. if (INVALID_ATTRS.includes(key)) {
  31. return;
  32. }
  33.  
  34. const event = parseEvent(key);
  35.  
  36. if (event) {
  37. events[event] = value;
  38. } else {
  39. bindings.push(key);
  40. }
  41. });
  42.  
  43. this.set('attributeBindings', bindings);
  44. this.set('eventBindings', events);
  45. },
  46.  
  47. _addEventListeners() {
  48. const events = this.get('eventBindings');
  49. const element = this.element;
  50.  
  51. Object.entries(events).forEach(([key, value]) => {
  52. element.addEventListener(key, value);
  53. });
  54. }
  55.  
  56. }).reopenClass({
  57. positionalParams: ['tagName']
  58. });
Add Comment
Please, Sign In to add comment