[w3c/webcomponents] Constructor arguments in custom elements (#605)

Dependency injection is a very well established pattern that greatly improves test-ability of a piece of software. Is common for components to use external code in the form of a dependency, like an API client for example which developers want to mock during tests. In a world of modules we shouldn't rely on global variables.  
This dependencies could be retrieved by the browser from a user defined function that returns an array(or any iterable?) of objects or primitives that are passed as constructor arguments when the custom element is instantiated.
```js
class MyComponent extends HTMLElement {
    constructor(dep1, dep2) {
        super()
        this.foo = dep1
        this.bar = dep2
    }
}

customElements.define('my-component', MyComponent, { dependencies: () => [new Foo, new Bar] })
```  

An alternative could be a function that allows users to instantiate elements manually. 
```js
customElements.define('my-component', MyComponent, { construct: MyComponent => new MyComponent(foo, bar) }) 
```

-- 
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/605

Received on Thursday, 10 November 2016 08:33:28 UTC