Re: [w3c/webcomponents] A new attribute similar to is="", but would allow multiple behaviors to be attached to a given element. (#662)

I mentioned 

> (unless there's a way to limit which elements a behavior can be applied to, but I'll skip that idea for now).

So here's that idea. The API might look like this:

```js
class Foo { ... }
elementBehaviors.define('foo', Foo, {limit: [HTMLMapElement, WebMap]})
```

Suppose we have this markup

```html
<web-map mixin="foo"></web-map>
<map mixin="foo"></map>
<div mixin="foo"></div>
```

In this case, only two instances of the `Foo` class would be created, for the `web-map` and `map` elements based on the interfaces they are defined with (built in or not). There would not be a `Foo` instance created for the `div` element, and also not for any other element besides those two map elements.

And here's another interesting idea. Suppose we have

```js
class SomeElement { ... }
customElements.define('some-element', SomeElement)
elementBehaviors.define('foo', Foo, {limit: [SomeElement]})
elementBehaviors.define('bar', Bar, {limit: [HTMLUnknownElement]})
```

This would apply the behavior only to any element that has no underlying class. For example, if there's no class defined for `some-element`,

```html
<some-element has="bar"></some-element>
```

then the `bar` behavior will be created for that element.

If at some point the element gets upgraded to a `SomeElement`, the `removedCallback` of the `bar` behavior will be called, then a new `Foo` behavior will be constructed and passed in the upgraded element. Interesting possibility!

-- 
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/662#issuecomment-328003591

Received on Friday, 8 September 2017 05:36:20 UTC