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

@treshugart Yeah I know, it's a well know fact that `document.createElement()` or the declarative API need a class with 0 arguments constructor and call super always because, how do you pass arguments to your constructor form the HTML? What I propose is a way to tell the browser that my custom element class has a constructor that needs some arguments and that it can get those arguments by calling the function that the user provided, then the browser will use that to correctly create the instance of the class... So yes it would be some standard way of doing DI now that you guys mention it, but still is it that bad or crazy?  
A patched `document.createElement` could be doing something like this:
```js 
const originalCreateElement = HTMLDocument.prototype.createElement
HTMLDocument.prototype.createElement = ( elementName, options ) => {
    const constructor = customElements.get( elementName )
    if ( constructor ) {
        // get arguments somewhere with a public API with a creative name
        let args = customElements.getRegisteredArgumentsForConstructor( constructor )
        // or from a well known symbol in the constructor? 
        // let args = constructor[elementDependenciesSymbol]
        return new constructor(...args)
    else {
         return originalCreateElement.call(this, elementName, options)
    }
}
```


-- 
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#issuecomment-259926804

Received on Friday, 11 November 2016 10:20:50 UTC