Event delegation not working in X-tag

170 Views Asked by At

I can't get delegation to work with events. I'm trying to have a certain function fire when a button within the element is pressed. However, when I use the syntax provided ('tap:delegate(button)': function(){});) I get no response. Below is some example code, Some help would be appreciated.

HTML:
<my-tag></my-tag>

Js:
xtag.register('my-tag', {
content: '<span>Some Text</span><button>my button</button>',
events: {
 'tap:delegate(button)': function(){
  console.log('the button was pushed');
}
} 
});
1

There are 1 best solutions below

0
On

Your code is working for me. Something could be loading out of order. You could try wrapping your code with a WebComponentsReady event handler:

window.addEventListener('WebComponentsReady', function () {
    xtag.register('my-tag', ...);
});

I usually put the script tags at the bottom of the <body> to avoid this.

<body>
  <my-tag></my-tag>

  <script src="lib/x-tag-core.js"></script>
  <script src="components/my-tag.js"></script><!-- xtag.register('my-tag', ...) -->
</body>

Or if you're using something like webpack

// components/my-tag.js

var xtag = require('~lib/x-tag-core.js');

module.exports = xtag.register('my-tag', ...);