Re: [w3c/webcomponents] Better custom event support for custom elements would be great (#546)

@chris-morgan you could have a simple convention like:
```js
class MyImage extends HTMLElement {

  static get observedAttributes() {
    return ['onerror', 'onload'];
  }

  attributeChangedCallback(name, oldValue, newValue) {
    if (/^on/.test(name)) {
      if (newValue) {
        this[name] = newValue;
        this.addEventListener(name.slice(2), this);
      } else {
        delete this[name];
        this.removeEventListener(name.slice(2), this);
      }
    } else {
      // every other attribute logic
    }
  }

  // any event previously registered through the instance
  // will pass through this listener
  handleEvent(e) {
    return this['on' + e.type](e);
  }

}
```

Since attributes are notified as soon as the element is upgraded, you'll have your events and, whenever such attribute will be removed, or changed, you'll have them reflected.

The good part of using the instance as listener is that you'll never end up with double listeners or never removed one (like unhanded bound events would be) so it's quite easy to circumvent the current limit when it comes to custom attributes, keeping the `on` prefix like the de-fact standard to define an event.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/546#issuecomment-247059876

Received on Wednesday, 14 September 2016 15:52:56 UTC